django - 如何使用 django 将表单数据从一个页面发送到另一个页面

标签 django forms django-forms django-views django-admin

  • 问题:我想将index.html的表单数据发送到greetings.html,该数据在greetings.html上显示用户名,但它显示错误消息

    NoReverseMatch at / Reverse for 'greeting.views.greetings' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: [] Request Method: GET . Can you suggest a simple method to send the form data to another page using django.

  • 我想要一个选择框来显示列表中存在的选项。在表单内。

greetings/template/index.html

{% load static %}
<!DOCTYPE html>
<html>
<head>
  <title> </title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" type="text/css" href="{% static 'greetings/css/style.css' %}" />
    {% verbatim %}
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    {% endverbatim %}
</head>
<body >
  <div class="container-fluid" >
    <div id="home" class="">
              <form class="input-form text-center" name = "form"  method = "POST"  action="{% url 'greetings.views.greetings'%}">
                  {% csrf_token %}
                <h1> Create your wishes </h1>
                  <input type="text" name = "username" placeholder="Enter your name"/>
                  <select name="select">
                      <option value=''>Please Choose your wish</option>
                  </select>
                  <br/>
                  <input id="btn" type="submit" name="submit" value="Create"/>
              </form>
    </div>
  </div>
</body>
</html>

greetings/template/greetings.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div id="demo-wish"class="card text-center">
              <h4> {{ username }} </h4>
           <h3> Wishes you </h3>
           <h4> Happy{{ username }} </h4>
          </div>
        <div id="demo" class="card text-center">
            <form>
                <h2>Create Your Wishes</h2>
                <h4>ENTER YOUR NAME TO WISH YOUR FRIENDS AND FAMILY MEMBERS</h4>
                <span>{{ username }}</span><br/>
                <input type="text" placeholder="Enter your name">
                <input type="submit" class="btn-primary"   value="Go">
            </form>
        </div>
      </div>
</body>
</html>

greetings/views.py

from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from greetings.forms import LoginForm


def index(request):
    template = loader.get_template('greetings/index.html')
    return HttpResponse(template.render(request))

def greetings(request):
    if request.method == "POST":
        # Get the posted form
        MyLoginForm = LoginForm(request.POST)

        if MyLoginForm.is_valid():
            username = MyLoginForm.cleaned_data['username']
    else:
        MyLoginForm = LoginForm()

    return render(request, 'greetings/greetings.html', {"username": username})

greetings/urls.py

from django.conf.urls import url,include
from django.contrib import admin
from . import views

urlpatterns = [
    url(r'^$', views.index),
    url(r'^admin/', admin.site.urls),
]

project/urls.py

from django.conf.urls import url,include
from django.contrib import admin

urlpatterns = [
    url(r'^$', include('greetings.urls')),
    url(r'^admin/', admin.site.urls),
]

greetings/forms.py

from django import forms

class LoginForm(forms.Form):
   occasion=['christmas','New Year']
   user = forms.CharField(max_length = 100)
   select = forms.ChoiceField(widget=forms.Select(choices=occasion))

Error-Message:

NoReverseMatch at /
Reverse for 'greeting.views.greetings' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Request Method: GET
Request URL:    http://127.0.0.1:8000/
Django Version: 1.10.4
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'greeting.views.greetings' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Exception Location: C:\Users\Captain America\AppData\Local\Programs\Python\Python36-32\lib\site-packages\django-1.10.4-py3.6.egg\django\urls\resolvers.py in _reverse_with_prefix, line 392
Python Executable:  C:\Users\Captain America\AppData\Local\Programs\Python\Python36-32\python.exe
Python Version: 3.6.0
Python Path:    
['D:\\django\\project_greetings',
 'C:\\Users\\Captain '
 'America\\AppData\\Local\\Programs\\Python\\Python36-32\\python36.zip',
 'C:\\Users\\Captain '
 'America\\AppData\\Local\\Programs\\Python\\Python36-32\\DLLs',
 'C:\\Users\\Captain '
 'America\\AppData\\Local\\Programs\\Python\\Python36-32\\lib',
 'C:\\Users\\Captain America\\AppData\\Local\\Programs\\Python\\Python36-32',
 'C:\\Users\\Captain '
 'America\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages',
 'C:\\Users\\Captain '
 'America\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\django-1.10.4-py3.6.egg']
Server time:    Sat, 7 Jan 2017 17:58:39 +0530

最佳答案

下面的行产生了错误

<form class="input-form text-center" name = "form"  method = "POST"  action="{% url 'greetings.views.greetings'%}">

但是当你查看 urls.py 时,你没有这样定义的 View 。所以你需要先定义它。然后你需要assign it a name如果你想获取带有 url 标签的链接

When you name your URL patterns, make sure you use names that are unlikely to clash with any other application’s choice of names. If you call your URL pattern comment, and another application does the same thing, there’s no guarantee which URL will be inserted into your template when you use this name.

事实上,我建议您阅读整个页面。在链接中。

关于django - 如何使用 django 将表单数据从一个页面发送到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41521293/

相关文章:

python - 覆盖 Django 中 ChoiceField 的初始化

python - Django IntegrityError-NOT NULL 约束失败 : portfolio_myportfolio.holder_id

javascript - Django 模板中的嵌入数据太大/未显示?

python - Django - DateTimeField 收到一个天真的日期时间

javascript - 添加视频 URL,带有实时预览选项

html - 在表单中排列字段

ios - iOS 中的 Django 管理过滤器小部件

Django 1.8 - 中间多对多直通关系 - 使用 'ManytoManyField' 的结果是什么?

python - 登录后如何重定向 django.contrib.auth.views.login?

ruby-on-rails - 未设置选择的 Rails 默认值?