报错1: 1 The view sign_up.views.new_user didn't return an HttpResponse object. It returned None instead.
views.py代码 1 2 3 4 5 6 7 8 9 10 11 def new_user (request) : if request.method != 'POST' : form = UserInfoForm() else : form = UserInfoForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(reverse('sign_up:index' )) context = {'form' :form} return render(request,'new_user.html' ,context)
models.py代码 1 2 3 4 5 6 7 8 class User_info (models.Model) : name = models.CharField(max_length=100 ) student_id = models.CharField(max_length=9 ) college = models.CharField(max_length=100 ) date_added = models.DateField(auto_now_add=True ) def __str__ (self) : return self.name
new_user.html代码 1 2 3 4 5 6 7 8 9 10 11 {% extends 'sign_up/index.html' %} {% block header %} <div > 请填写信息</div > {% endblock %} {% block content %} <form action ="{% url 'sign_up:new_user' %}" method ="post" > {% csrf_token %} {% form.as_p %} <button type ="submit" > Add user</button > </form > {% endblock %}
错误原因:当时报错时查询各种资料以及函数都没有解决。。。最后发现是由于views.py文件中的没有返回值,return错位导致。
正确代码如下:
1 2 3 4 5 6 7 8 9 10 11 def new_user (request) : if request.method != 'POST' : form = UserInfoForm() else : form = UserInfoForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect(reverse('sign_up:index' )) context = {'form' :form} return render(request,'new_user.html' ,context)
报错2:
1 Invalid block tag on line 8: 'form.as_p', expected 'endblock'. Did you forget to register or load this tag?
错误原因:前端模版中form.as_p应该放在1 2 3 4 5 6 7 8 9 10 {{ }} ```中,而不是`{% %}`中 ---- ### 报错3: ![这里写图片描述](/images/在搭建报名系统时遇到的django报错/2B46DB12-FF96-4472-BB3D-4C5BFC6059C4.png) ```python no such table: sign_up_actor_info
错误原因:应当使用python manage.py makemigrations
以及python manage.py migrate
来迁移数据库以及修改数据库即可
报错4: 1 Specifying a namespace in include() without providing an app_name
错误原因:python3 Django 环境下,如果你遇到namespace没有注册以及在根目录下urls.py中的include方法的第二个参数namespace添加之后就出错的问题。请在[app_name]目录下的urls.py中的urlpatterns前面加上app_name=’[app_name]’, [app_name]代表你的应用的名称。
报错5: 错误原因:将根目录下的1 url(r'^users/$',include('users.urls',namespace='users')),
中的$
删除
报错6:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 @login_required def new_actor (request) : if request.method != 'POST' : form = ActorInfoForm() else : form = ActorInfoForm(data=request.POST) if form.is_valid(): new_actor = form.save() new_actor.owner = request.user new_actor.save() return HttpResponseRedirect(reverse('sign_up:index' )) context = {'form' :form} return render(request, 'sign_up/new_actor.html' , context)
错误原因:在第一次form.save时应该是用参数commit=False,使其不上传到数据库,因为后面还要对其信息进行添加
正确代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 @login_required def new_actor (request) : if request.method != 'POST' : form = ActorInfoForm() else : form = ActorInfoForm(data=request.POST) if form.is_valid(): new_actor = form.save(commit=False ) new_actor.owner = request.user new_actor.save() return HttpResponseRedirect(reverse('sign_up:index' )) context = {'form' :form} return render(request, 'sign_up/new_actor.html' , context)
报错7:
1 'QuerySet' object has no attribute 'owner'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 @login_required def edit_actor (request,actor_id) : actor = Actor_info.objects.filter(id=actor_id) if actor.owner != request.user: raise Http404 if request.method == 'POST' : college = request.POST.get('college' ) student_id = request.POST.get('student_id' ) name = request.POST.get('name' ) Actor_info.objects.filter(id=actor_id).update( college=college,student_id=student_id,name=name ) return HttpResponseRedirect(reverse('sign_up:actor' ,args={'actor_id' :actor_id})) context = {'actor' :actor} return render(request,'sign_up/edit_actor.html' ,context=context)
错误原因:应该使用get而不是filter
正确代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 @login_required def edit_actor (request,actor_id) : actor = Actor_info.objects.get(id=actor_id) if actor.owner != request.user: raise Http404 if request.method == 'POST' : college = request.POST.get('college' ) student_id = request.POST.get('student_id' ) name = request.POST.get('name' ) Actor_info.objects.filter(id=actor_id).update( college=college,student_id=student_id,name=name ) return HttpResponseRedirect(reverse('sign_up:actor' ,args=[actor_id])) context = {'actor' :actor} return render(request,'sign_up/edit_actor.html' ,context=context)
报错8:
1 2 Reverse for 'actor' with no arguments not found. 1 pattern(s) tried: ['actors/(?P<actor_id>\\d+)/$' ] Request Method: POST
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 @login_required def edit_actor (request,actor_id) : actor = Actor_info.objects.get(id=actor_id) if actor.owner != request.user: raise Http404 if request.method == 'POST' : college = request.POST.get('college' ) student_id = request.POST.get('student_id' ) name = request.POST.get('name' ) Actor_info.objects.filter(id=actor_id).update( college=college,student_id=student_id,name=name ) return HttpResponseRedirect(reverse('sign_up:actor' ),args=[actor_id]) context = {'actor' :actor} return render(request,'sign_up/edit_actor.html' ,context=context)
错误原因:args为reverse函数的参数,而不是HttpResponseRedirect的参数
正确代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 @login_required def edit_actor (request,actor_id) : actor = Actor_info.objects.get(id=actor_id) if actor.owner != request.user: raise Http404 if request.method == 'POST' : college = request.POST.get('college' ) student_id = request.POST.get('student_id' ) name = request.POST.get('name' ) Actor_info.objects.filter(id=actor_id).update( college=college,student_id=student_id,name=name ) return HttpResponseRedirect(reverse('sign_up:actor' ,args=[actor_id])) context = {'actor' :actor} return render(request,'sign_up/edit_actor.html' ,context=context)