I’m facing my first encoding-related bug and I’m stumped. The problem is in a Django app with a form which updates existing database entries. When the update form is used to make changes to an entry, I’m finding that the new entry data includes encoding-related notations like quotation marks, parentheses, and the u (encoding mark). It appears that I’ve made some sort of mistake which confuses Django about what encoding my strings are in.
To summarize, this is the problem:
Initial state: Entry title field is FooBar Desired state: Entry title field is FooBar2 What I get instead: Entry title field is (u’FooBar2’,) These encoding markers are getting printed out in my Django app, and I confirmed that they're also there when I access the database directly from the Python shell (as in the example which follows)>>>entry.title
u"(u'FooBar2',)"
Any idea why my form and view are saving these notations into the database? How can I stop it?
This is the form I’m using to edit the database entries:
class EntryEditForm(forms.Form):
title = forms.CharField(label=u'Entry title.', max_length=100)
target = forms.CharField(label=u'Entry target', required=False)
username = forms.CharField(label=u'Entry-specific username', max_length=100, required=False)
password = forms.CharField(label=u'Entry-specific password', max_length=100, required=False)
This is the view which is used to edit the database entries:
def editentry_page(request):
if request.method == 'POST':
form = EntryEditForm(request.POST)
entryid = unquote(request.POST['entryid'])
if form.is_valid():
entry = request.user.entry_set.get(id=entryid)
entry.title=form.cleaned_data['title'],
entry.username=form.cleaned_data['username'],
entry.password=form.cleaned_data['password'],
entry.targetemail=form.cleaned_data['targetemail'],
entry.user=User.objects.get(username=request.user)
entry.save()
return HttpResponseRedirect('/user/%s/' % request.user.username)
elif 'entryid' in request.GET:
entryid = unquote(request.GET['entryid'])
try:
selectedentry = request.user.entry_set.get(id=entryid)
title = selectedentry.title
targetemail = selectedentry.targetemail
username = selectedentry.username
password = selectedentry.password
except (Entry.DoesNotExist):
raise Http404(u'This entry does not exist or is not your entry')
entry = Entry.objects.get(id=entryid)
form = EntryEditForm({
'title': entry.title,
'email': entry.email,
'target': entry.target,
'username': entry.username,
'password': entry.password,
})
else:
raise Http404(u'No entry selected - return to your entries to try again.')
variables = RequestContext(request, {'form': form, ‘entryid': unquote(request.GET['entryid']),})开发者_运维知识库
return render_to_response('entry_edit.html', variables)
This is the entry model
class Entry(models.Model):
title = models.CharField(max_length=200)
email = models.EmailField(unique=True)
password = models.CharField(max_length=200)
username = models.CharField(max_length=200)
target = models.CharField(unique=False)
user = models.ForeignKey(User)
entry.title=form.cleaned_data['title'],
The final comma is turning it into a tuple in your view. Stop doing that.
精彩评论