繼上次找到是rouning問題之後,已經有解決了,感謝提供建議和提示.
這次又來發問請教,因為實在是想不透這錯誤到底是什麼 @@
我是使用Python+Django做網頁開發,正在練習寫"編輯 Edit"功能
但在資料驗證時候try except,卻發生錯誤導致直接跳到except結束,結果也就是沒有任何編輯完並存檔.
有問題的行數在 View.py的第195行,這到底是為什麼資料傳不過去到變數...@@
錯誤訊息如下圖
ERR:
View:
@login_required(login_url='/admin/login/')
def edit_task(request,id=1):
logged_user = User.objects.get(username=request.user.username)
logged_user = UserProfile.objects.get(user=logged_user)
unit= TaskList.objects.all()
if request.method == "POST":
etask = request.POST.get("task")
try:
# task = TaskList.objects.get(task=etask) #這行資料型態傳進來有問題?!
target_edit = TaskList.objects.get(task=etask)
target_edit.task = etask
target_edit.save()
except Exception as e:
print(e)
return redirect("/tasklist/")
try:
target_edit = TaskList.objects.get(id=id)
except:
return redirect("/tasklist/")
return render(request,"edit_task.html",locals())
Template:
<form action="/edittask/" method="POST">
{% csrf_token %}
<table class="table table-striped">
<tr>
<td align=right>待辦事項</td>
<td><input type=text name="task" size=100 value='{{target_edit.task}}' required></td>
</tr>
<tr><td> </td><td>
<input type=submit value="確定" class="btn btn-primary">
</td></tr>
</table>
</form>
Model:
class TaskList(models.Model):
task = models.CharField(max_length=200)
priority = models.PositiveIntegerField(default=0)
status = models.PositiveIntegerField(default=0) # 0:未處理,100:已完成
user = models.CharField(max_length=50)
worker = models.CharField(max_length=50, null=True, blank=True)
timestamp = models.DateTimeField(default=timezone.now)
start_date = models.DateField(null=True, blank=True)
finish_date = models.DateField(null=True, blank=True)
def __str__(self):
return self.task
錯誤訊息:TaskList matching query does not exist.
顧名思義就是找不到符合條件的資料
程式寫的是這列tasks = TaskList.objects.get(task=etask)
其中 etask 的值是「測試編」
TaskList 的值在左邊的 unit 有(iT邦這裡看不到)
你可以自己檢查看看 unit 裡
是否沒有符合條件(task=測試編)的物件
另外在下有一事不明
既然寫了unit = TaskList.objects.all()
為何之後不用 unit
仍然使用 TaskList.objects.get
1.unit的內容,是傳進來接受到的值
2.既然寫了unit = TaskList.objects.all()
為何之後不用 unit
仍然使用 TaskList.objects.get
其實是可以用"unit"沒錯,只是個人方便閱讀code @@''
我是參考這寫法
@login_required(login_url='/admin/login/')
def edit_member(request, id=1):
logged_user = User.objects.get(username=request.user.username)
logged_user = UserProfile.objects.get(user=logged_user)
teams = TeamName.objects.all()
titles = Title.objects.all()
if request.method=="POST":
email = request.POST.get("email")
title_name = request.POST.get("title")
team_name = request.POST.get("team")
phone = request.POST.get("phone")
try:
title = Title.objects.get(name=title_name)
team = TeamName.objects.get(name=team_name)
target = User.objects.get(email=email)
targetprofile = UserProfile.objects.get(user=target)
targetprofile.title=title
targetprofile.team=team
targetprofile.phone=phone
targetprofile.save()
except Exception as e:
print(e)
return redirect("/members/")
try:
target_userprofile = UserProfile.objects.get(id=id)
target_user = User.objects.get(id=target_userprofile.user.id)
except:
return redirect("/members/")
return render(request, "edit-member.html", locals())