django常见问题,随手记
Q1:将Debug=false后,所有页面都打不开,全部显示server error 500,但是设置Debug=true又好了
可能报错:CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False.
解决方案:ALLOWED_HOSTS = [‘127.0.0.1‘, ‘localhost‘](如果是部署到公网,则填写对应域名或者子域名)
Q2:报错信息: ModelForm Creating a ModelForm without either the ‘fields‘ attribute or the ‘exclude‘ attribute is prohibited; form AuthorForm needs updating.
解决方案:
1 2 3 4 |
class AuthorForm(ModelForm): class Meta: model = Author fields = "__all__" |
Q3:报错信息: CSRF token missing or incorrect
解决方法:A1:
Step 1: 在 templete 中, 为每个 POST form 的form标签内部增加一个 {% csrf_token %} tag.
Step 2: 在 view 中, 使用 django.template.RequestContext 而不是 Context.render_to_response, 默认使用 Context. 需要改成 RequestContext.
1 2 |
return render_to_response(‘systemofdingh/modify.html‘, {‘form‘: orderModelForm(instance=orderModify)}, context_instance=RequestContext(request)) |
解决方法: A2 :
1 2 |
1. settings.py 中 MIDDLEWARE_CLASSES 中 注释掉‘django.middleware.csrf.CsrfViewMiddleware‘ 2. 在你的views.py 的方法上加上 @csrf_exempt 装饰 (需要 from django.views.decorators.csrf import csrf_exempt),第二步,我没有改,但是已经可行。 |
原创文章,作者:ifyoung,如若转载,请注明出处:https://www.drugfoodai.com/django-exception-1.html