当我尝试通过python / django中的表单添加项目时,出现白色错误屏幕。我正在尝试调试它,但是没有信息。有人可以指出我正确的方向吗?
Models.py
from __future__ import unicode_literals
from django.db import models
# Create your models here.
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.utils import timezone
# Create your models here.
class UserProfile(models.Model):
image = models.ImageField(upload_to='images', default='Upload Picture')
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name="profile")
first_name = models.CharField(max_length=255, blank=True)
last_name = models.CharField(max_length=255, blank=True)
address1 = models.CharField(max_length=255, blank=True)
address2 = models.CharField(max_length=255, blank=True)
city = models.CharField(max_length=20, null=True)
county = models.CharField(max_length=100, null=True)
postcode = models.CharField(max_length=7, null=True)
biography = models.TextField(max_length=280,blank=True)
email = models.CharField(max_length=50, blank=True, null=True)
phone = models.CharField(max_length=10, blank=True)
dob = models.CharField(max_length=10, blank=True)
gender = models.CharField(max_length=1, blank=True)
facebook = models.CharField(max_length=50, blank=True, null=True)
twitter = models.CharField(max_length=50, blank=True, null=True)
instagram = models.CharField(max_length=50, blank=True, null=True)
class KidProfile(models.Model):
parent = models.ForeignKey(User, related_name='kids')
name = models.CharField(max_length=255, null=True, blank=True)
dob = models.CharField(max_length=10, null=True, blank=True)
gender = models.CharField(max_length=1, null=True, blank=True)
needs = models.CharField(max_length=3, null=True, blank=True)
def __str__ (self):
return self.name
views.py
from django.contrib import messages, auth
from accounts.forms import UserRegistrationForm, UserLoginForm, FullUserDetailsForm, KidDetailsForm
from django.core.urlresolvers import reverse
from django.shortcuts import render, redirect, HttpResponseRedirect, HttpResponse, get_object_or_404
from django.template.context_processors import csrf
from django.contrib.auth.decorators import login_required
from .models import KidProfile, UserProfile
@login_required(login_url='/accounts/login')
def profile(request):
kids = KidProfile.objects.filter(parent=request.user)
adults = UserProfile.objects.filter(user=request.user)
return render(request, 'profile.html', {'kids': kids}, {'adults': adults})
def update_profile(request):
form=FullUserDetailsForm(request.POST, request.FILES)
if form.is_valid():
request.user.first_name=form.cleaned_data['first_name']
request.user.last_name=form.cleaned_data['last_name']
request.user.email=form.cleaned_data['email']
request.user.profile.address1=form.cleaned_data['address1']
request.user.profile.address2=form.cleaned_data['address2']
request.user.profile.postcode=form.cleaned_data['postcode']
request.user.profile.phone=form.cleaned_data['phone']
request.user.profile.dob=form.cleaned_data['dob']
request.user.profile.gender=form.cleaned_data['gender']
request.user.profile.facebook=form.cleaned_data['facebook']
request.user.profile.twitter=form.cleaned_data['twitter']
request.user.profile.instagram=form.cleaned_data['instagram']
request.user.save()
return redirect(reverse('profile'))
else:
return HttpResponse("Error")
def update_profile_kid(request, id):
kid = get_object_or_404(KidProfile, pk=id)
form=KidDetailsForm(request.POST, request.FILES)
if form.is_valid():
kid.name=form.cleaned_data['name']
kid.dob=form.cleaned_data['dob']
kid.gender=form.cleaned_data['gender']
kid.needs=form.cleaned_data['needs']
kid.save()
return redirect(reverse('profile'))
else:
return HttpResponse("Error")
def create_profile_kid(request):
form=KidDetailsForm(request.POST, request.FILES)
if form.is_valid():
kid=KidProfile()
kid.name=form.cleaned_data['name']
kid.dob=form.cleaned_data['dob']
kid.gender=form.cleaned_data['gender']
kid.needs=form.cleaned_data['needs']
kid.parent=request.user
kid.save()
return redirect(reverse('profile'))
else:
return HttpResponse("Error")
def delete_profile_kid(request, id):
kid = get_object_or_404(KidProfile, pk=id)
kid.delete()
return redirect(reverse('profile'))
def login(request):
if request.method == 'POST':
form = UserLoginForm(request.POST)
if form.is_valid():
user = auth.authenticate(username=request.POST.get('username_or_email'),
password=request.POST.get('password'))
if user is not None:
auth.login(request, user)
messages.error(request, "You have successfully logged in")
if request.GET and request.GET['next'] !='':
next = request.GET['next']
return HttpResponseRedirect(next)
else:
return redirect(reverse('babysitters'))
else:
form.add_error(None, "Your username or password was not recognised")
else:
form = UserLoginForm()
args = {'form': form, 'next': request.GET['next'] if request.GET and 'next' in request.GET else ''}
args.update(csrf(request))
return render(request, 'login.html', args)
def logout(request):
auth.logout(request)
messages.success(request, 'You have successfully logged out')
return redirect(reverse('index'))
def register(request):
if request.method == 'POST':
form = UserRegistrationForm(request.POST)
if form.is_valid():
form.save()
user = auth.authenticate(username=request.POST.get('username'),
password=request.POST.get('password1'))
if user:
auth.login(request, user)
messages.success(request, "You have successfully registered")
return redirect(reverse('profile'))
else:
messages.error(request, "unable to log you in at this time!")
else:
form = UserRegistrationForm()
args = {'form': form}
return render(request, 'profile.html', args)
urls.py
from django.conf.urls import url
from .views import register, profile, logout, login, update_profile, update_profile_kid, create_profile_kid, delete_profile_kid
urlpatterns = [
url(r'^register/$', register, name='register'),
url(r'^profile/$', profile, name='profile'),
url(r'^profile/update/$', update_profile, name='update_profile'),
url(r'^profile/kids/update/(?P<id>\d+)$', update_profile_kid, name='update_profile_kid'),
url(r'^profile/kids/delete/(?P<id>\d+)$', delete_profile_kid, name='delete_profile_kid'),
url(r'^profile/kids/create/$', create_profile_kid, name='create_profile_kid'),
url(r'^logout/$', logout, name='logout'),
url(r'^login', login, name='login'),
]
我正在尝试调试问题,但到目前为止没有成功。有人可以帮助我或为我指明正确的方向。
答案 0 :(得分:1)
在某些情况下,您的视图仅返回字符串“ Error”,而没有任何信息。那就是你所看到的。
您永远不应该return HttpResponse("Error")
,因为那样只会向浏览器返回带有文本“ Error”的文档。
当表单无效时,您应该只返回与绑定的无效表单相同的页面,并向用户显示表单错误。
阅读Forms documentation,尤其是如何正确创建视图以正确处理表单的三种状态(未绑定,绑定无效和绑定有效)。
通常,一个视图应该这样处理:
request.method
是GET
,则创建未绑定的空表格(或带有一些initial
值)。然后,您使用此表单返回模板,以便用户填写。request.method
是POST
,则创建绑定表格并检查其是否有效。
GET
情况相同)。现在,您的模板将显示包含用户先前填写的值和错误的表单,以便用户可以更正它们并重试。