在django中用户输入非常简单 [英] Very simple user input in django

查看:163
本文介绍了在django中用户输入非常简单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的基础斗争是我很难理解django模板,视图和网址绑在一起...什么是最简单的,最小的最小方式提示用户输入一个字符串,然后使用该字符串来查询数据库(最好w / python模型不是raw sql查询)?我应该使用GET和POST方法吗?我应该使用表格吗?我需要使用模板,还是可以使用通用视图?



当我尝试提交输入,只是重新加载输入页。



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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆