如何在 Django 视图中获取 POST 请求值?

发布于 2024-12-10 08:24:55 字数 911 浏览 0 评论 0原文

我正在尝试通过 html/django 制作注册表单,因此我有 3 个输入框供用户输入电子邮件、用户名和密码,然后通过 POST 将它们发送到 /adduser

<form action="/OmniCloud_App/adduser" method="post">
{% csrf_token %}
  Email Address: <input type="text" name="email" /></br>
  Username: <input type="text" name="username" maxlength=25 /></br>
  Password: <input type="password" maxlength=30 /></br>
  </br>
  <input type="submit" value="Send" /> <input type="reset">
</form>

adducer 创建一个新用户并将其保存到DB:

def adduser(request, email, username, password):
    u = User(email=email, username=username, password=password)
    u.save()
    return render_to_response('adduser.html', {'email':email, 'username':username, 'password':password})

但是当我单击 /signup 上的“提交”时,它抱怨我只给了它 1 个参数,而预期是 3 个参数。我应该如何将电子邮件、用户名和密码字段从 signup.html 传递到用户名函数(位于 /username)?

I'm trying to make a signup form via html/django so I have 3 input boxes for the user to put in the email, username, and password that then sends them via POST to /adduser

<form action="/OmniCloud_App/adduser" method="post">
{% csrf_token %}
  Email Address: <input type="text" name="email" /></br>
  Username: <input type="text" name="username" maxlength=25 /></br>
  Password: <input type="password" maxlength=30 /></br>
  </br>
  <input type="submit" value="Send" /> <input type="reset">
</form>

adducer creates a new User and saves it to the DB:

def adduser(request, email, username, password):
    u = User(email=email, username=username, password=password)
    u.save()
    return render_to_response('adduser.html', {'email':email, 'username':username, 'password':password})

but when I click submit on /signup, it complains that I am only giving it 1 parameter when 3 were expected. How should I pass the email,username, and password fields from signup.html to the username function (located at /username)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最佳男配角 2024-12-17 08:24:55

如果您阅读本教程的第 3 部分,您会看到该视图函数需要 URL 本身的一部分作为参数。如果您阅读同一教程的第 4 部分,您会看到 POST参数通过 request.POST 传入。在文档中,您将了解到可以编写处理 HTML 表单的生成和验证的 Form 类。

If you read part 3 of the tutorial, you'll see that the view function expects parts of the URL itself as arguments. If you read part 4 of the same tutorial, you'll see that POST parameters come in via request.POST. Further in the documentation, you'll learn that you can write Form classes that handle both the generation and validation of HTML forms.

虐人心 2024-12-17 08:24:55

它们将位于 request.POST 中,您可以像查询 dict 一样对其进行查询

email = request.POST.get('email')
username = request.POST.get('username')
password = request.POST.get('password')

they will be in request.POST, which you can query like you would a dict

email = request.POST.get('email')
username = request.POST.get('username')
password = request.POST.get('password')
在巴黎塔顶看东京樱花 2024-12-17 08:24:55

例如,如果您在 index.html 中提交 POST 请求值,如下所示:

{# "index.html" #}

<form action="{% url 'my_app1:test' %}" method="post">
  {% csrf_token %}
  <input type="text" name="fruits" value="apple" /></br>
  <input type="text" name="meat" value="beef" /></br>
  <input type="submit" />
</form>

那么,您可以在 index.html 中获取 POST 请求值。 code>my_app1/views.py 如下所示。 *我的回答解释了更多:

# "my_app1/views.py"

from django.shortcuts import render

def test(request):

    print(request.POST['fruits']) # apple
    print(request.POST.get('meat')) # beef
    print(request.POST.get('fish')) # None
    print(request.POST.get('fish', "Doesn't exist")) # Doesn't exist
    print(request.POST.getlist('fruits')) # ['apple']
    print(request.POST.getlist('fish')) # []
    print(request.POST.getlist('fish', "Doesn't exist")) # Doesn't exist
    print(request.POST._getlist('meat')) # ['beef']
    print(request.POST._getlist('fish')) # []
    print(request.POST._getlist('fish', "Doesn't exist")) # Doesn't exist
    print(list(request.POST.keys())) # ['csrfmiddlewaretoken', 'fruits', 'meat']
    print(list(request.POST.values())) # ['b0EQnFlWoAp4pUrmsFxas43DYYTr7k04PhhYxqK3FDTBSXWAkJnsCA3GiownZQzS', 'apple', 'beef']
    print(list(request.POST.items())) # [('csrfmiddlewaretoken', 'b0EQnFlWoAp4pUrmsFxas43DYYTr7k04PhhYxqK3FDTBSXWAkJnsCA3GiownZQzS'), ('fruits', 'apple'), ('meat', 'beef')]
    print(list(request.POST.lists())) # [('csrfmiddlewaretoken', ['b0EQnFlWoAp4pUrmsFxas43DYYTr7k04PhhYxqK3FDTBSXWAkJnsCA3GiownZQzS']), ('fruits', ['apple']), ('meat', ['beef'])]
    print(request.POST.dict()) # {'csrfmiddlewaretoken': 'b0EQnFlWoAp4pUrmsFxas43DYYTr7k04PhhYxqK3FDTBSXWAkJnsCA3GiownZQzS', 'fruits': 'apple', 'meat': 'beef'}
    print(dict(request.POST)) # {'csrfmiddlewaretoken': ['b0EQnFlWoAp4pUrmsFxas43DYYTr7k04PhhYxqK3FDTBSXWAkJnsCA3GiownZQzS'], 'fruits': ['apple'], 'meat': ['beef']}

    return render(request, 'test.html')

For example, if you submit the POST request values in index.html as shown below:

{# "index.html" #}

<form action="{% url 'my_app1:test' %}" method="post">
  {% csrf_token %}
  <input type="text" name="fruits" value="apple" /></br>
  <input type="text" name="meat" value="beef" /></br>
  <input type="submit" />
</form>

Then, you can get the POST request values in my_app1/views.py as shown below. *My answer explains it more:

# "my_app1/views.py"

from django.shortcuts import render

def test(request):

    print(request.POST['fruits']) # apple
    print(request.POST.get('meat')) # beef
    print(request.POST.get('fish')) # None
    print(request.POST.get('fish', "Doesn't exist")) # Doesn't exist
    print(request.POST.getlist('fruits')) # ['apple']
    print(request.POST.getlist('fish')) # []
    print(request.POST.getlist('fish', "Doesn't exist")) # Doesn't exist
    print(request.POST._getlist('meat')) # ['beef']
    print(request.POST._getlist('fish')) # []
    print(request.POST._getlist('fish', "Doesn't exist")) # Doesn't exist
    print(list(request.POST.keys())) # ['csrfmiddlewaretoken', 'fruits', 'meat']
    print(list(request.POST.values())) # ['b0EQnFlWoAp4pUrmsFxas43DYYTr7k04PhhYxqK3FDTBSXWAkJnsCA3GiownZQzS', 'apple', 'beef']
    print(list(request.POST.items())) # [('csrfmiddlewaretoken', 'b0EQnFlWoAp4pUrmsFxas43DYYTr7k04PhhYxqK3FDTBSXWAkJnsCA3GiownZQzS'), ('fruits', 'apple'), ('meat', 'beef')]
    print(list(request.POST.lists())) # [('csrfmiddlewaretoken', ['b0EQnFlWoAp4pUrmsFxas43DYYTr7k04PhhYxqK3FDTBSXWAkJnsCA3GiownZQzS']), ('fruits', ['apple']), ('meat', ['beef'])]
    print(request.POST.dict()) # {'csrfmiddlewaretoken': 'b0EQnFlWoAp4pUrmsFxas43DYYTr7k04PhhYxqK3FDTBSXWAkJnsCA3GiownZQzS', 'fruits': 'apple', 'meat': 'beef'}
    print(dict(request.POST)) # {'csrfmiddlewaretoken': ['b0EQnFlWoAp4pUrmsFxas43DYYTr7k04PhhYxqK3FDTBSXWAkJnsCA3GiownZQzS'], 'fruits': ['apple'], 'meat': ['beef']}

    return render(request, 'test.html')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文