Python django 中非常简单的用户输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25028895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Very simple user input in django
提问by Rick James
My underlying struggle is I am having trouble understanding how django templates, views, and urls are tied together... What is the simplest, bare minimum way to prompt the user to input a string, then use that string to query a database (preferably w/ python model not raw sql queries)? Should I use GET and POST methods? Should I use a form? Do I need to use a template or can I use a generic view?
我的根本问题是我无法理解 django 模板、视图和 url 是如何绑定在一起的......提示用户输入字符串,然后使用该字符串查询数据库的最简单、最低限度的方法是什么(最好是带 python 模型而不是原始 sql 查询)?我应该使用 GET 和 POST 方法吗?我应该使用表格吗?我需要使用模板还是可以使用通用视图?
when i try submitting input it just reloads the input page.
当我尝试提交输入时,它只会重新加载输入页面。
views.py:
视图.py:
from django.shortcuts import render
from django.shortcuts import HttpResponse
from People.models import Person
def index(request):
return render(request, 'People/index.html')
def search(request):
search_id = request.POST.get('textfield', None)
try:
user = Person.objects.get(MAIN_AUTHOR = search_id)
#do something with user
html = ("<H1>%s</H1>", user)
return HttpResponse(html)
except Person.DoesNotExist:
return HttpResponse("no such user")
urls.py
网址.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^People/', 'People.views.index'),
url(r'^People/send/', 'People.views.search'),
)
template:
模板:
<form method="POST" action="send/">
{% csrf_token %}
<input type="text" name="textfield">
<button type="submit">Upload text</button>
</form>
Am I missing something or doing something wrong?
我是否遗漏了什么或做错了什么?
采纳答案by Archit Verma
If I understand correctly, you want to take some input from the user, query the database and show the user results based on the input. For this you can create a simple django form that will take the input. Then you can pass the parameter to a view in GETrequest and query the database for the keyword.
如果我理解正确,您想从用户那里获取一些输入,查询数据库并根据输入显示用户结果。为此,您可以创建一个简单的 django 表单来接收输入。然后您可以将参数传递给GET请求中的视图并查询数据库中的关键字。
EDIT: I have edited the code. It should work now.
编辑:我已经编辑了代码。它现在应该可以工作了。
views.py
视图.py
from django.shortcuts import render
from django.shortcuts import HttpResponse
from .models import Person
from django.core.exceptions import *
def index(request):
return render(request, 'form.html')
def search(request):
if request.method == 'POST':
search_id = request.POST.get('textfield', None)
try:
user = Person.objects.get(name = search_id)
#do something with user
html = ("<H1>%s</H1>", user)
return HttpResponse(html)
except Person.DoesNotExist:
return HttpResponse("no such user")
else:
return render(request, 'form.html')
urls.py
网址.py
from django.conf.urls import patterns, include, url
from People.views import *
urlpatterns = patterns('',
url(r'^search/', search),
url(r'^index/', index)
)
form.html
表单.html
<form method="POST" action="/search">
{% csrf_token %}
<input type="text" name="textfield">
<button type="submit">Upload text</button>
</form>
Also make sure that you place your templates in a seperate folder named templatesand add this in your settings.py:
还要确保将模板放在一个名为的单独文件夹中templates,并将其添加到您的settings.py:
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__), '../templates').replace('\','/'),
)
回答by Alexander
For a user input you'll need 2 views - one to display the page with the form and another to process the data. You hook the first view to one url, say "feedback/", and the other one to a url like "feedback/send/". You also need to specify this second url in your form tag.
对于用户输入,您需要 2 个视图 - 一个用于显示带有表单的页面,另一个用于处理数据。您将第一个视图连接到一个 url,比如“feedback/”,另一个视图连接到一个 url,如“feedback/send/”。您还需要在表单标签中指定第二个 url。
<form method="POST" action="feedback/send/">
<input type="text" name="textfield">
...
<button type="submit">Upload text</button>
</form>
Now in the second view you can obtain the form data and do whatever you want with it.
现在在第二个视图中,您可以获取表单数据并对其进行任何操作。
def second_view(request):
if request.method == "POST":
get_text = request.POST["textfield"]
# Do whatever you want with the data
Take a look at this page Fun with Forms. It'll give you the basic understanding. I would also advice to work through the whole book. You should use ether GET or POST (GET is probably not secure). Using form is not mandatory, as you can style it and perform all the validation yourself and then pass the data straight to the model.
看看这个页面Fun with Forms。它会给你一个基本的了解。我还建议通读整本书。您应该使用 ether GET 或 POST(GET 可能不安全)。使用表单不是强制性的,因为您可以设置它的样式并自己执行所有验证,然后将数据直接传递给模型。

