在django中用户输入非常简单 [英] Very simple user input in django
问题描述
当我尝试提交输入,只是重新加载输入页。
views.py:
从django.shortcuts import render
从django.shortcuts import HttpResponse
来自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用户
html =(<
返回HttpResponse(html)
除了Person.DoesNotExist:
return HttpResponse(no such user)
urls.py
from django.conf.urls import pattern,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'),
)
模板:
; form method =POSTaction =send />
{%csrf_token%}
< input type =textname =textfield>
< button type =submit>上传文本< / button>
< / form>
我错过了某些东西或做错了吗?
如果我理解正确,您需要从用户那里获取一些输入,查询数据库并根据输入显示用户的结果。为此,您可以创建一个简单的django表单,以获取输入。然后,您可以将参数传递给 GET
请求中的视图,并查询关键字的数据库。
编辑:
我已经编辑了代码。它应该现在工作。
views.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用户
html =(< H1>%s< / H1>,用户)
返回HttpResponse(html)
除了Person.DoesNotExist:
return HttpResponse(no such user
else:
return render(request,'form.html')
urls.py
from django.conf.urls import patterns,include,url
从People.views import *
urlpatterns = patterns('',
url(r'^ search /',search),
url(r'^ index /',index )
)
form.html
< form method =POSTaction =/ search>
{%csrf_token%}
< input type =textname =textfield>
< button type =submit>上传文本< / button>
< / form>
还要确保将模板放在名为 templates
并将其添加到您的 settings.py
中:
TEMPLATE_DIRS =(
os.path.join(os.path.dirname(__ file__),'../templates').replace('\\\','/'),
)
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?
when i try submitting input it just reloads the input page.
views.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
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?
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 GET
request and query the database for the keyword.
EDIT: I have edited the code. It should work now.
views.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
from django.conf.urls import patterns, include, url
from People.views import *
urlpatterns = patterns('',
url(r'^search/', search),
url(r'^index/', index)
)
form.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 templates
and add this in your settings.py
:
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__), '../templates').replace('\\','/'),
)
这篇关于在django中用户输入非常简单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!