在搭建報名系統時遇到的django報錯
報錯1:
The view sign_up.views.new_user didn't return an HttpResponse object. It returned None instead.
views.py程式碼
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程式碼
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程式碼
{% 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錯位導致。
正確程式碼如下:
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:
Invalid block tag on line 8: 'form.as_p', expected 'endblock'. Did you forget to register or load this tag?
錯誤原因:前端模版中form.as_p應該放在{{ }}中,而不是{% %}中
報錯3:
no such table: sign_up_actor_info
錯誤原因:應當使用python manage.py makemigrations
以及`python manage.py migrate
來遷移資料庫以及修改資料庫即可
報錯4:
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:
![這裡寫圖片描述](//img-blog.csdn.net/20180313203700214?watermark/2/text/Ly9ibG9nLmNzZG4ubmV0L3FxXzM4NTQyMDg1/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
錯誤原因:將根目錄下的url(r'^users/$',include('users.urls',namespace='users')),
中的$刪除
報錯6:
@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,使其不上傳到資料庫,因為後面還要對其資訊進行新增
正確程式碼如下:
@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) #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:
'QuerySet' object has no attribute 'owner'
@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
正確程式碼如下:
@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:
Reverse for 'actor' with no arguments not found. 1 pattern(s) tried: ['actors/(?P<actor_id>\\d+)/$']
Request Method: POST
@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的引數
正確程式碼如下:
@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)
相關推薦
在搭建報名系統時遇到的django報錯
報錯1: The view sign_up.views.new_user didn't return an HttpResponse object. It returned None instead. views.py程式碼 def n
採用分散式系統架構,使用dubbo時xml檔案報錯
在採用分散式系統架構時,我們會經常使用到阿里巴巴的dubbo的分散式框架。 在相關xml配置了dubbo的約束依賴後,即使能上網eclipse、myeclipse等IDE也是無法識別dubbo的相關約
1、webpack搭建專案時出現的報錯“Module build failed (from ./node_modules/css-loader/dist/cjs.js) CssSyntaxError”
控制檯出現的錯誤如下: webpack.config.js中的配置如下: module.exports={ entry:'./src/main.js
使用POI操作Excel時new XSSFWorkbook ()報錯java.lang.NoSuchMethodError解決方式
lin line java sts factor 出現 class padding test 使用最新的POI3.11時,在執行 Workbook workBook = new XSSFWorkbook ();這段代碼時出現錯誤: java.lang.NoSu
django 報錯 no such table: auth_user
overflow pan cnblogs http nag .com erro django ati 需要執行 python3 manage.py makemigrations python3 manage.py migrate 參考:http://arr
scala中計算的的一個小問題,超出Int.maxValue時不會報錯
class sca cal div 問題 pri 如果 int sin 如果小於Int.max時他不會報錯 def sumcount(str:String): Int ={ val ints = for(c <- str)yield { p
ReactNative環境搭建擴展篇——安裝後報錯解決方案
每次 搖一搖 ack pst img 工程 項目 python2 -1 之前一篇寫了《邏輯性最強的React Native環境搭建與調試》說了RN的安裝,今天在這裏做一個復盤,讓我們能夠更直觀更深入的了解React Native(以下簡稱RN),這一篇重點來說的就是,安裝之
使用maven創建springMVC時返回頁面報錯
jdk1 pil maven 分享 apache art 解決 編譯 img 這是由於你的 Maven 編譯級別是 jdk1.5 或以下,而你導入了 jdk1.6 以上的依賴包 解決辦法: <build> <finalName&g
appium_v1.4.16版本不適配android7.0系統,運行報錯“Attempt to re-install io.appium.settings without first uninstalling”
urn fail tin ins 找到 auto his ger 問題: 要解決的問題:appium在androidV7.0系統上運行時報錯 Failure [INSTALL_FAILED_ALREADY_EXISTS: Attempt to re-install io.a
vue-cli打包構建時常見的報錯解決方案
imu cli blog error: 背景 服務 strong 背景圖 log 報錯1:打包後丟到服務器中,打開是空白頁 報錯2:打包後想要在本地file中打開,但是打開是空白頁 報錯3:打包後在瀏覽器中打開,報錯ERROR in xxx.js from UglifyJs
CS11:2008R2 Hyper-V 系統自帶備份報錯0x8100010處理
etime 方法 category 操作 2008r2 resolve net serve spp 客戶問題概括: 客戶在2008R2 Hyper-V 主機上用自帶Windows Server Backup 備份時出現錯誤無法備份,在Hyper-V主機上發現如下日誌. Lo
使用ffmpeg轉碼時遇到aac報錯
允許 aac 實驗 解碼器 inf 錯誤 ffmpeg 視頻 ant 今天嘗試用ffmpeg轉一個視頻的格式,結果報出這個錯誤: The encoder 'aac' is experimental but experimental codecs are n
ORA-15025 搭建DG環境,restore controlfile報錯,提示oracle無法使用ASM存儲
red over 二階 ora- clas 11.2.0.3 occurred and add 環境說明: #主庫RAC環境 #備庫RAC環境,操作系統AIX 6.1 數據庫版本11.2.0.3 報錯說明: #主庫備份控制文件,傳輸至備庫,備庫restore 報錯
Django報錯 No module named 'django.templates'
django site set 工程文件 .py module nbsp 方法 name 前言 Django 模板報錯了 修改方法: 將你的工程文件下(my_site)的settings.py中的TEMPLATES中的templates字段全部改為template
當子查詢內存在ORDER BY 字句時查詢會報錯
問題: highlight div 數據 order use 解決辦法 內存 true 問題:當子查詢內存在ORDER BY 字句時查詢會報錯 SQL: SELECT * FROM ( SELECT * FROM USER ORDER BY USER_CORD
django報錯must be str, not dict
request cloud vars reat var get code itl except Django出現報錯 TypeError at /main/mmp/workticket/create/app/ must be str, not dict Request Me
python3 Django 報錯RuntimeWarning的解決辦法
blog true RoCE packages img ima als zone 圖片 我們在shell下運行Django的時候遇到這樣的報錯: C:\python\python3\lib\site-packages\django\db\backends\sqlite3\b
linux系統中修改密碼報錯的問題
ali 解決 ria 字典 pre ren sele show 規則 1、mysql密碼報錯的原因 報錯信息 ERROR 1819 (HY000): Your password does not satisfy the current policy requirement
01 React快速入門(一)——使用迴圈時對於‘key’報錯處理
問題描述: 在剛開始接觸react學習的時候,編寫一個小功能時,使用了map來迴圈一個數組中的資料,輸出到前端頁面,程式碼除錯執行後正常顯示,但是開啟控制檯卻發現有一條關於“key”的報錯訊息,詳細資訊如下: &
Django中urls與path的區別,啟動Django報錯400
啟動django後,不能訪問,報400錯誤。 原因:沒有開啟允許訪問 處理:編輯HelloWorld目錄下setting.py ,把其中的 ALLOWED_HOSTS=[]改成ALLOWED_HOSTS=['*'] ##* 表示任意地址。 在使用D