[英]Saving Django Form Data
I am learning Django forms and am trying to save form data. 我正在学习Django表单,并试图保存表单数据。 I have a working form, but I can't figure out to 'do' anything with the data entered on the form.
我有一个有效的表单,但是我无法弄清楚对表单上输入的数据执行任何操作。 Specifically, I am trying to do the following two things:
具体来说,我正在尝试做以下两件事:
First , once the user submits the form, load a new page that states: "You searched for 'X'". 首先 ,一旦用户提交了表单,就加载一个新页面,该页面指出:“您搜索了'X'”。
Second , have the form data interact with an existing database. 其次 ,让表单数据与现有数据库进行交互。 Specifically, I have a model called 'Hashtag' that has two attributes: 'search_text' and 'locations'.
具体来说,我有一个名为“ Hashtag”的模型,该模型具有两个属性:“ search_text”和“ locations”。 I think the process would work as follows:
我认为该过程如下:
Where, 哪里,
X = user-inputted form data X =用户输入的表格数据
Y = hashtag.locations.all() in a list Y =列表中的hashtag.locations.all()
Thus far, I have the below: 到目前为止,我有以下内容:
models.py models.py
from django.db import models
class Hashtag(models.Model):
"""
Model representing a specific hashtag search. The model contains two attributes:
1) a search_text (eg 'trump') for which there will be only one for database entry (the row),
2) a list of locations (eg ['LA, CA', 'LA, CA', 'NY, NYC', 'London, UK', 'London, United Kingdom']) for which there may be 0+ per search_text.
"""
search_text = models.CharField(max_length=140, primary_key=True)
locations = models.TextField()
def __str__(self):
""" String for representing the Model object (search_text) """
return self.search_text
def display_locations(self):
""" Creates a list of the locations """
# ISSUE: insert correct code, something like: return '[, ]'.join(hastagsearch.location_list for location in self.location.all())
pass
forms.py forms.py
from django import forms
from django.forms import ModelForm
from .models import Hashtag
class SearchHashtagForm(ModelForm):
""" ModelForm for user to search by hashtag """
def clean_hashtag(self):
data = self.cleaned_data['search_text']
# Check search_query doesn't include '#'. If so, remove it.
if data[0] == '#':
data = data[1:]
# return the cleaned data
return data
class Meta:
model = Hashtag
fields = ['search_text',]
labels = {'search_text':('Hashtag Search'), }
help_texts = { 'search_text': ('Enter a hastag to search.'), }
views.py views.py
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.urls import reverse
from .models import Hashtag
from .forms import SearchHashtagForm
def hashtag_search_index(request):
""" View for index page for user to input search query """
hashtag_search = get_object_or_404(Hashtag)
# If POST, process Form data
if request.method == 'POST':
# Create a form instance and populate it with data from request (binding):
form = SearchHashtagForm(request.POST)
# Check if form is valid
if form.is_valid():
# process the form data in form.cleaned_data as required
hashtag_search.search_text = form.cleaned_data['search_text']
# the reason we can use .save() is because we associated the form with the model as a ModelForm
hashtag_search.save()
# redirect to a new URL
return HttpResponseRedirect(reverse('mapping_twitter:hashtag_search_query'))
# If GET (or any other method), create the default form
else:
form = SearchHashtagForm()
context = {'hashtag_search':hashtag_search, 'form':form}
return render(request, 'mapping_twitter/hashtag_search_query.html', context)
I am considering that a potential way to achieve this is to create another model and save the user-inputted form data there. 我正在考虑实现此目的的一种潜在方法是创建另一个模型并将用户输入的表单数据保存在那里。 I am wondering whether that is correct, and how that solution could be used to achieve the Second stated goal above :)
我想知道这是否正确,以及该解决方案如何用于实现上述第二个目标:)
Thanks and apologies in advance if my explanation is a mess/plain wrong :/ 如果我的解释是一团糟/简单的错误,请提前致谢和歉意:/
EDIT 编辑
The EDIT below has made the following changes: 下面的EDIT进行了以下更改:
def results()
def results()
models.py models.py
from django.db import models
class Location(models.Model):
""" Model representing a Location, attached to Hashtag objects through a
M2M relationship """
name = models.CharField(max_length=140)
def __str__(self):
return self.name
class Hashtag(models.Model):
""" Model representing a specific Hashtag serch, containing two attributes:
1) A `search_text` (fe 'trump'), for which there will be only one per
database entry,
2) A list of `locations` (fe ['LA, CA', 'NY, NYC']), for which there
may be any number of per `search_text` """
search_text = models.CharField(max_length=140, primary_key=True)
locations = models.ManyToManyField(Location, blank=True)
def __str__(self):
""" String for representing the Model object (search_text) """
return self.search_text
def display_locations(self):
""" Creates a list of the locations """
# Return a list of location names attached to the Hashtag model
return self.locations.values_list('name', flat=True).all()
views.py views.py
...
def results(request):
""" View for search results for `locations` associated with user-inputted `search_text` """
search_text = hashtag_search
location_list = Hashtag.display_locations()
context = {'search_text':search_text, 'location_list':location_list}
return render(request, 'mapping_twitter/results.html')
The full repo can be found here: https://github.com/darcyprice/Mapping-Data 完整的回购可以在这里找到: https : //github.com/darcyprice/Mapping-Data
EDIT 2 编辑2
The EDIT below makes the following changes: 下面的EDIT进行以下更改:
def results()
def results()
的建议修订 Although I copied directly from the Mozilla tutorial ( https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Forms ), I suspect that the line: hashtag_search.search_text = form.cleaned_data['search_text']
doesn't correctly store hashtag_search
. 尽管我直接从Mozilla教程( https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/Forms )复制,但我怀疑这一行:
hashtag_search.search_text = form.cleaned_data['search_text']
没有正确存储hashtag_search
。
ERROR 错误
NameError at /search_query/
name 'hashtag_search' is not defined
Request Method: POST
Request URL: http://ozxlitwi.apps.lair.io/search_query/
Django Version: 2.0
Exception Type: NameError
Exception Value:
name 'hashtag_search' is not defined
Exception Location: /mnt/project/mapping_twitter/views.py in hashtag_search_index, line 24
Python Executable: /mnt/data/.python-3.6/bin/python
Python Version: 3.6.5
Python Path:
['/mnt/project',
'/mnt/data/.python-3.6/lib/python36.zip',
'/mnt/data/.python-3.6/lib/python3.6',
'/mnt/data/.python-3.6/lib/python3.6/lib-dynload',
'/usr/local/lib/python3.6',
'/mnt/data/.python-3.6/lib/python3.6/site-packages']
views.py views.py
def hashtag_search_index(request):
""" View for index page for user to input search query """
# If POST, process Form data
if request.method == 'POST':
# Create a form instance and populate it with data from request (binding):
form = SearchHashtagForm(request.POST)
# Check if form is valid
if form.is_valid():
hashtag_search.search_text = form.cleaned_data['search_text']
hashtag_search.save()
# redirect to a new URL
return HttpResponseRedirect(reverse('mapping_twitter:results'))
# If GET (or any other method), create the default form
else:
form = SearchHashtagForm()
context = {'hashtag_search':hashtag_search, 'form':form}
return render(request, 'mapping_twitter/hashtag_search_index.html', context)
def results(request):
""" View for search results for `locations` associated with user-inputted `search_text` """
search_text = hashtag_search
location = get_object_or_404(Hashtag, search_text=search_text)
location_list = location.display_locations()
context = {'search_text':search_text, 'location_list':location_list}
return render(request, 'mapping_twitter/results.html', context)
Turn the locations
attribute into a M2M field. 将
locations
属性转换为M2M字段。 That sounds like what you need here. 听起来像您这里需要的。 Keep in mind that this is untested code.
请记住,这是未经测试的代码。
models.py models.py
from django.db import models
class Location(models.Model):
""" A model representing a Location, attached to Hashtag objects through a Many2Many relationship """
name = models.CharField(max_length=140)
def __str__(self):
return self.name
class Hashtag(models.Model):
"""
Model representing a specific hashtag search. The model contains two attributes:
1) a search_text (eg 'trump') for which there will be only one for database entry (the row),
2) a list of locations (eg ['LA, CA', 'LA, CA', 'NY, NYC', 'London, UK', 'London, United Kingdom']) for which there may be 0+ per search_text.
"""
search_text = models.CharField(max_length=140, primary_key=True)
locations = models.ManyToManyField(Location)
def __str__(self):
""" String for representing the Model object (search_text) """
return self.search_text
def display_locations(self):
""" Creates a list of the locations """
# This will return a list of location names attached to the Hashtag model
return self.locations.values_list('name', flat=True).all()
views.py views.py
...
def results(request):
""" View for search results for `locations` associated with user-inputted `search_text` """
search_text = hashtag_search
location = get_object_or_404(Hashtag, search_text=search_text)
location_list = location.display_locations()
context = {'search_text':search_text, 'location_list':location_list}
return render(request, 'mapping_twitter/results.html')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.