![](/img/trans.png)
[英]Django Rest Framework - Why am I getting an error when the function isn't even supposed to do anything
[英]Why Django's POST doesn't do anything - not even an error?
我正在從 Python/Django 為 Uni 做一個作業。 我必須做的一件事是創建一個表單,用戶可以在其中在網站上創建新的“錦標賽”。 前端根本不重要。
我創建了一個模型,從管理面板添加了一些錦標賽,效果很好。 但是當我嘗試從表單創建一個新錦標賽並單擊 sumbit 按鈕時,我被重定向到我的主頁(即使 HTML 或 views.py 中沒有指定應該發生這種情況),我沒有收到錯誤,沒有已發布數據,但沒有從命令行返回信息。
class Tournament(models.Model):
title = models.CharField(max_length=30)
creator = models.OneToOneField(User, on_delete=models.CASCADE)
players = models.ManyToManyField(User, related_name="players",)
created_date = models.DateField(auto_now=True)
start_date = models.DateField(auto_now=True)
max_players = models.IntegerField(
null=True, validators=[MinValueValidator(2), MaxValueValidator(64)])
slug = models.SlugField(unique=True, db_index=True)
def __str__(self):
return f"{self.title} \n {self.creator} \n {self.max_players}"
class TournamentForm(forms.ModelForm):
class Meta:
model = Tournament
#exclude = ["slug"]
fields = "__all__"
class TournamentView(View):
def get(self, request):
form = TournamentForm()
print(form.errors)
return render(request, "tournament_app/new_tournament.html", {
"form": form
})
def post(self, request):
form = TournamentForm(request.POST)
if form.is_valid():
print(form.errors)
form.save()
return redirect("/thank-you")
print(form.errors)
return render(request, "tournament_app/new_tournament.html", {
"form": form
})
{% extends "base.html" %}
{% block title %} Create New tournament {% endblock title %}
{% extends "base.html" %}
{% block title %}
Create New tournament
{% endblock title %}
{% block content %}
<form action="/" method="POST">
{% csrf_token %}
{% for field in form%}
<div class="form-control">
{{field.label_tag}}
{{field}}
</div>
{% endfor %}
<button type="submit">Send</button>
</form>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}{% endblock title %}</title>
</head>
<body>
{% if user.is_authenticated %}
<p style="text-align:right;">You are logged in as <b>{{user.username}}</b></p>
{% else %}
<p style="text-align:right;"><b>Anonymous user</b></p>
{% endif %}
<a href="{% url "index-page" %}"><button type = "button"> Main Page </button></a>
<a href="{% url "login" %}"><button type = "button"> Login </button></a>
{% block content %}{% endblock content %}
</body>
</html>
正如您在 views.py 中看到的,我嘗試至少檢查幾年前其他一些帖子中建議的任何錯誤,但我沒有得到任何回復。 在命令行中,我看到它是這樣的:
[22/Jul/2021 13:33:10] "GET /new-tournament HTTP/1.1" 200 1839
[22/Jul/2021 13:33:19] “POST / HTTP/1.1” 200 744
我完全沒有 WebDev 的經驗,也沒有 Django 的經驗。 你能幫我找出問題嗎? 如果至少有一些錯誤響應或類似的東西。
我被重定向到我的主頁(即使在 HTML 或 views.py 中沒有指定這應該發生)
哦,但你很明確地指定了這一點。 :-)
您發布到/
,而不是/new-tournament
(您的 TournamentView)。
<form action="/" method="POST">
擺脫的action
張貼到當前的URL來代替。
<form method="POST">
此外,您可以將 TournamentView 簡化為CreateView
:
class TournamentView(CreateView):
model = Tournament
template_name = "tournament_app/new_tournament.html"
success_url = "/thank-you"
(是的,這應該是全部)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.