问题:我想将index.html的表单数据发送到在greetings.html上显示用户名的greetings.html,但是它显示错误消息
NoReverseMatch位于/
找不到带有参数“()”和关键字参数“ {}”的“ greeting.views.greetings”。尝试了0个模式:[]
请求方法:GET
。你能建议一个简单的
使用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})
问候/ 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),
]
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))
错误信息:
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时,就没有这样定义的视图。因此,您需要先定义它。然后,如果要获取带有url标签的链接,则需要assign it a name
命名网址格式时,请确保使用以下名称
不太可能与其他应用程序的名称选择发生冲突。如果你
调用您的URL模式注释,另一个应用程序执行相同的操作
事情,不能保证将哪个URL插入您的
使用此名称时的模板。
实际上,我建议您阅读整个页面。在链接中。