Admin User model

huangapple go评论68阅读模式
英文:

Admin User model

问题

我的管理员用户模型在使用UserCreationForm注册时不会更新新记录。

访问 GitHub 代码

在视图中,我已经导入了UserCreationForm,并进行了所有的视图编码。我不知道哪里出错了。我还正确地将表单渲染到模板中。但麻烦的部分是在填写表单后。一旦我访问它,它就不会在管理员面板中更新。

// 视图文件

from django.shortcuts import render
from django.http import HttpResponse
from django.contrib.auth.forms import UserCreationForm

def home(request):
    if request.method == "POST":
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
    context = {'form': form}
    return render(request, 'main/index.html', context)

// 模板文件

<!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>Document</title>
</head>
<body>
    <form action="" method="POST">
        {% csrf_token %}
        {{ form.as_p }}
        <input type="submit" value="register">
    </form>
</body>
</html>
英文:

My admin user model does not update new record when I register using UserCreationForm.

Access the github code

in the views I have imported Usercreationform has shown below and I did all the views coding. I do not know where I went wrong. I also rendered the form correctly into the template. But the annoying part comes after filling the form. It doesn't update in the admin panel once I accessed it

// View file:

from django.shortcuts import render
from django.http import HttpResponse
from django.contrib.auth.forms import UserCreationForm

def home(request):
    if request.method == &quot;POST&quot;:
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
    context = {&#39;form&#39;:form}
    return render(request, &#39;main/index.html&#39;, context)

// Template file:

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;
    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
    &lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;form action=&quot;&quot; method=&quot;POST&quot;&gt;
        {% csrf_token %}
        {{form.as_p}}
        &lt;input type=&quot;submit&quot; value=&quot;register&quot;&gt;
    &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

答案1

得分: 1

以下是您要翻译的内容:

An excerpt from the docs:

You should always return an HttpResponseRedirect after successfully dealing with POST data. This tip isn’t specific to Django; it’s good web development practice in general.

So try the following view:

from django.shortcuts import redirect

def home(request):
    if request.method == "POST":
        form = UserCreationForm(request.POST)
        if form is_valid():
            form.save()
            return redirect("success_page")
        else:
            return redirect("some_error_page")
    else:
        form = UserCreationForm()
    return render(request, 'main/index.html', {'form': form})

def success(request):
    return render(request, 'main/success.html')

Your urls.py should be:


urlpatterns=[
    path('', v.home, name='home')
    path('success/', v.success, name='success_page')
]

success.html

<body>
    <h1> The form has been successfully submitted. </h1>
    
    <a href="{% url 'home' %}"> Go to homepage </a>
</body>

The same URL pattern and HTML page, you can make for displaying error if the form is not valid.

You can also remove empty action attribute as Django by default takes the current page route so the template should be like:

<form method="POST">
        {% csrf_token %}
        {{form.as_p}}
        <input type="submit" value="register">
</form>
英文:

An excerpt from the docs:

> You should always return an HttpResponseRedirect after successfully dealing with POST data. This tip isn’t specific to Django; it’s good web development practice in general.

So try the following view:

from django.shortcuts import redirect

def home(request):
    if request.method == &quot;POST&quot;:
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect(&quot;success_page&quot;)
        else:
            return redirect(&quot;some_error_page&quot;)
    else:
        form = UserCreationForm()
    return render(request, &#39;main/index.html&#39;, {&#39;form&#39;:form})

def success(request):
    return render(request, &#39;main/success.html&#39;)

Your urls.py should be:


urlpatterns=[
    path(&#39;&#39;, v.home, name=&#39;home&#39;)
    path(&#39;success/&#39;, v.success, name=&#39;success_page&#39;)
]

success.html

&lt;body&gt;
    &lt;h1&gt; The form has been successfully submitted. &lt;/h1&gt;
    
    &lt;a href=&quot;{% url &#39;home&#39; %}&quot;&gt; Go to homepage &lt;/a&gt;
&lt;/body&gt;

The same URL pattern and HTML page, you can make for displaying error if the form is not valid.

You can also remove empty action attribute as Django by default takes current page route so the template should be like:

&lt;form method=&quot;POST&quot;&gt;
        {% csrf_token %}
        {{form.as_p}}
        &lt;input type=&quot;submit&quot; value=&quot;register&quot;&gt;
&lt;/form&gt;

huangapple
  • 本文由 发表于 2023年3月3日 22:02:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/75628064.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定