Получение ошибки при размещении нового заказа. Работало нормально до соединения с пользователем в моделях. Когда я создаю отдельное представление user_page и связываю модель клиента с пользователем (импорт из django .contrib.auth.models), он дает следующую ошибку:
TypeError at / create_order / str возвращено нестроковый (тип NoneType)
Код в представлениях
@login_required(login_url='login')
def user_page(request):
orders=Customer.objects.filter(user=request.user)
context={'orders':orders}
return render(request, 'blog/user_page.html', context)
@unauthenticated_user
def registration_page(request):
if request.method=='POST':
form=CreationUserForm(request.POST)
if form.is_valid():
user=form.save()
group=Group.objects.get(name='customer')
user.groups.add(group)
Customer.objects.create(
user=user,
)
return redirect('login')
else:
form=CreationUserForm()
return render(request, 'blog/registration.html', {'form':form})
@login_required(login_url='login')
@admin_only
def home_page(request):
orders=Order.objects.all()
customer_data=Customer.objects.all()
total_orders=orders.count()
pending=Order.objects.filter(status='PENDING')
total_pending=pending.count()
out = orders.filter(status='OUTFORDELIEVERY')
total_out = out.count()
delievered = orders.filter(status='DELIEVERED')
total_delievered = delievered.count()
context={'orders':orders, 'customer_data':customer_data, 'total_orders':total_orders, 'total_pending': total_pending,
'total_out':total_out, 'total_delievered':total_delievered}
return render(request, 'blog/home_page.html', context)
@login_required(login_url='login')
@allowed_users(allowed_roles=['admin'])
def product_page(request):
return render(request, 'blog/product.html')
@login_required(login_url='login')
@allowed_users(allowed_roles=['admin'])
def customer_page(request, id):
customer=Customer.objects.get(id=id)
orders=customer.order_set.all()
total_orders=orders.count()
myFilter=OrderFilter(request.GET, queryset=orders)
orders=myFilter.qs
context={'customer':customer, 'total_orders':total_orders, 'orders':orders, 'myFilter':myFilter}
return render(request, 'blog/customer.html', context)
@login_required(login_url='login')
def create_order(request):
if request.method=='POST':
form=CustomerForm(request.POST)
if form.is_valid():
form.save()
return redirect('/')
else:
form=CustomerForm()
context={'form':form}
return render(request, 'blog/new_order.html', context)
Код в моделях
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Customer(models.Model):
user=models.OneToOneField(User, null=True, on_delete=models.CASCADE)
name=models.CharField(max_length=200, null=True)
email=models.EmailField()
phone=models.IntegerField(null=True)
date_created=models.DateTimeField(auto_now_add=True, null=True)
def __str__(self):
return self.name
class Tag(models.Model):
name=models.CharField(max_length=200, null=True)
def __str__(self):
return self.name
class Product(models.Model):
name=models.CharField(max_length=200, null=True)
CATEGORY=(
('INDOOR','INDOOR'),
('OUTDOOR','OUTDOOR')
)
category=models.CharField(max_length=200, null=True, choices=CATEGORY)
date_created=models.DateTimeField(auto_now_add=True, null=True)
price=models.CharField(max_length=200, null=True)
tag=models.ManyToManyField(Tag)
class Order(models.Model):
customer=models.ForeignKey(Customer, null=True, on_delete=models.SET_NULL)
product=models.ForeignKey(Product, null=True, on_delete=models.SET_NULL)
date_created=models.DateTimeField(auto_now_add=True, null=True)
STATUS=(
('PENDING','PENDING'),
('OUTFORDELIEVERY','OUTFORDELIEVERY'),
('DELIEVERED','DELIEVERED')
)
status=models.CharField(max_length=200, choices=STATUS, null=True)
note=models.CharField(max_length=200, null=True)
order_no=models.CharField(max_length=200, null=True)