gpt4 book ai didi

python - 使用 MySQL 的 Django Web 框架从请求对象获取数据

转载 作者:行者123 更新时间:2023-11-29 21:24:37 25 4
gpt4 key购买 nike

专家您好, 我是 django 新手,正在尝试学习如何为 MySQL 数据库构建 django Web 框架。我可以发布我的查询(搜索词)并获得所需的结果。但我正在尝试修改我的项目,以便用户可以在提交页面提交查询,并在执行时在 URL 中查看其查询参数。像这样的东西:提交页面:http://localhost:8000/执行后页面如下:http://localhost:8000/xtrack/?searchid=XXXX

但是花了几天时间我仍然无法弄清楚如何以正确的方式做到这一点。

表单.py

from django import forms
from models import Query

class SQLForm(forms.ModelForm):
xtrackid=forms.CharField(max_length=100)
def checkxID(self):
xtrackid=self.cleaned_data.get("xtrackid")
return xtrackid

class QueryForm(forms.ModelForm):
class Meta:
model=Query
fields=["xtrackid"]

View .py

from django.shortcuts import render
from django.http import HttpResponse
from forms import SQLForm, QueryForm
import sys

def search_form(request):
return render(request, 'index.html')

def search(request):
form = QueryForm(request.POST or None)
if form.is_valid():
instance = form.save(commit=False)
xtrackid = form.cleaned_data.get("xtrackid")
xtrackid =xtrackid.strip()
conn = MySQLdb.connect (host = "localhost", user = "root", passwd = "XXXX", db = "XXXtracker")
cursor = conn.cursor ()
cursor.execute ("SELECT xInfo.xtideID, xIDunID.AccessionNumber FROM xInfo, xIDunID WHERE xInfo.xtideID = xIDunID.xtideID AND xIDunID.xtideID LIKE '%" + xtrackid +"%'")
row = cursor.fetchone ()
listrow= list(row)
contextres={}
if cursor.rowcount==0:
contexterror = {
'outputerror': xtrackid
}
return render(request, 'errorform.html', contexterror)
else:
if contextres.has_key(str(listrow[0])):
contextres[str(listrow[0])].append(listrow[1])
else:
contextres[str(listrow[0])]= [listrow[1]]
resulstdict = {'contextresultinfo': contextres}
return render(request, 'resultform.html', {'xinfo': resulstdict, 'query': xtrackid})
conn.close()

else:
return HttpResponse('Please submit a valid search term.')

url.py

from django.conf.urls import include, url
from django.contrib import admin
from myapp import views

urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^xtrack/$', views.search_form),
url(r'^resultform/$', views.search),
url(r'^errorform/$', views.search)

]

我的模板如下:索引.html

<html>
<h1> Welcome to xTrack </h1>
<head>
<title>Search</title>
</head>
<body>
<form action="/xtrack/" method="get">
<input type="text" name="xtrackid">
<input type="submit" value="Search">
</form>
</body>
</html>

结果表单.html

结果

{% if contextresultinfo %}
<table border="1" style="width:100%">
<tr>
<td>xtide tracker ID<br> </td>
<td>Accession number<br></td>
</tr>
{% for key, values in contextresultinfo.items %}
<tr>
{% for items in values %}
<tr>
<td>{{key}}</td>
{% for data in items %}
<td>{{data}}</td>
{% endfor %}
</tr>
{% endfor %}
</tr>
{% endfor %}
</table>
{% else %}
<p>No xtrack matched your search criteria.</p>
{% endif %}
</body>

您能给我一些想法,我需要在项目中更改代码吗?谢谢

最佳答案

在您看来,您通过以下方式获取提交数据:

form = QueryForm(request.POST or None)

但是在您的 html 文件中,您将表单方法定义为:

<form action="/peptrack/" method="get">

因此 request.POST 将无法获取任何数据。

关于python - 使用 MySQL 的 Django Web 框架从请求对象获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35568681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com