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

时间:2017-01-07 12:09:20

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

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

    NoReverseMatch at /   反向' greeting.views.greetings'参数'()'和关键字参数' {}'未找到。尝试过0种模式:[]   请求方法:GET   。你能说一个简单的吗?   使用django将表单数据发送到另一个页面的方法。

  • 我想要一个选择框,显示列表中的选项。在表格内。
  

问候/模板/ 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.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>
  

问候/ 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})
  

问候/ 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),
]
  

项目/ 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),
]
  

问候/ 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))
  

错误-消息:

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

1 个答案:

答案 0 :(得分:2)

这是产生错误的以下行

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

但是当你看到你的urls.py时,你没有这样定义的视图。所以你需要先定义它。如果你想通过url标签

获取链接,那么你需要assign it a name
  

为URL模式命名时,请确保使用的是名称   不太可能与任何其他应用程序的名称选择冲突。如果你   调用您的URL模式注释,另一个应用程序也这样做   事情,无法保证将哪个URL插入您的   使用此名称时的模板。

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