簡體   English   中英

為什么 Django 的 POST 什么都不做——甚至沒有錯誤?

[英]Why Django's POST doesn't do anything - not even an error?

我正在從 Python/Django 為 Uni 做一個作業。 我必須做的一件事是創建一個表單,用戶可以在其中在網站上創建新的“錦標賽”。 前端根本不重要。

我創建了一個模型,從管理面板添加了一些錦標賽,效果很好。 但是當我嘗試從表單創建一個新錦標賽並單擊 sumbit 按鈕時,我被重定向到我的主頁(即使 HTML 或 views.py 中沒有指定應該發生這種情況),我沒有收到錯誤,沒有已發布數據,但沒有從命令行返回信息。

模型.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}"

表單.py

class TournamentForm(forms.ModelForm):
    class Meta:
        model = Tournament
        #exclude = ["slug"]
        fields = "__all__"

視圖.py

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
        })

new_tournament.html

{% 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM