django常見小問題

1、當我把 DEBUG = True設爲False的時候運行 python manage.py runserver 的時候
報錯 : CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False.
解決方案:
ALLOWED_HOSTS = [‘127.0.0.1‘, ‘localhost‘]

2、報錯信息: ModelForm Creating a ModelForm without either the ‘fields‘ attribute or the ‘exclude‘ attribute
is prohibited; form AuthorForm needs updating.
解決方案:
class AuthorForm(ModelForm):
class Meta:
model = Author
fields = “all

3、報錯信息: CSRF token missing or incorrect
第一種方法: 第一步、 在 templete 中, 爲每個 POST form 增加一個 {% csrf_token %} tag. 如下:

{% csrf_token %}

第二步、在 view 中, 使用 django.template.RequestContext 而不是 Context.
render_to_response, 默認使用 Context. 需要改成 RequestContext.
eturn render_to_response(‘systemofdingh/modify.html‘, {‘form‘: orderModelForm(instance=orderModify)},
context_instance=RequestContext(request))

第二中方法:1. settings.py 中 MIDDLEWARE_CLASSES 中 註釋掉‘django.middleware.csrf.CsrfViewMiddleware‘
2. 在你的views.py 的方法上加上 @csrf_exempt 裝飾 (需要 from django.views.decorators.csrf import csrf_exempt)

4、 You are trying to add a non-nullable field ‘orderID‘ to ordermodel without a default; 
          we can‘t do that (the database needs something to populate existing rows).

Please select a fix:
1) Provide a one-off default now (will be set on all existing rows)
2) Quit, and let me add a default in models.py
解決方案: 刪除migrate文件,然後重新來

Django :

queryset的長度查詢 : len(queryset)

model對象轉換成dict : model_to_dict

發佈了43 篇原創文章 · 獲贊 12 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章