my post method returns (u'') and django saves includes the (u'') string when saving it
This is how I retrieve the post data from the webpage. The person models can be saved but it includes the "(u'')"
string. For example if change the firstname
to "Alex", it gets the raw value u('Alex')
and saves it.
def submit_e(req, person_id=None):
if(req.POST):
try:
person_id = req.POST['driver']
person = Person.objects.get(pk=person_id)
person.firstname = req.POST['firstname'],
person.midname = req.POST['middleinitial'],
person.lastname = req.POST['lastname'],
person.full_clean()
person.save()
except Exception as e:
print e
return HttpResponseRedirect(reverse('users:user_main'))
Asked by: Vivian405 | Posted: 30-11-2021
Answer 1
NB: the following is my best guess at what you are seeing based on your question. If I have guessed wrongly, the please update your post with more details - putting print
statements throughout your code and adding the output to your post would be a good start.
The u
prefix on a string indicates a Unicode string. It is not actually part of the contents of the string. If we create a string in the interpreter:
>>> name = u'Me'
and then request details of the string,
>>> name
u'Me'
then the u
is shown as it is part of the information about the string, which is what we have requested. If we print the contents of the string
>>> print name
Me
then the u
is not shown (just like the quotes aren't shown).
Using the interpreter to try and reproduce your problem, I created a new user with a Unicode string for a username:
>>> from django.contrib.auth.models import User
>>> new_user = User()
>>> new_user.username = u'Me'
>>> new_user.save()
And as before, if we request the details about the string we see the u
and the quotes, but if we print the contents of the string we don't:
>>> new_user.username
u'Me'
>>> print new_user.username
>>> Me
To further confirm the u
was not stored, we can explore the database directly:
sqlite> select username from auth_user;
Me
Answered by: Daryl834 | Posted: 01-01-2022
Answer 2
you need to delete the "," at the end of each line so, before:
person.firstname = req.POST['firstname'],
person.midname = req.POST['middleinitial'],
person.lastname = req.POST['lastname'],
after
person.firstname = req.POST['firstname']
person.midname = req.POST['middleinitial']
person.lastname = req.POST['lastname']
Answered by: Ryan754 | Posted: 01-01-2022
Similar questions
Large Python Includes
I have a file that I want to include in Python but the included file is fairly long and it'd be much neater to be able to split them into several files but then I have to use several include statements.
Is there some way to group together several files and include them all at once?
import - Python includes, module scope issue
I'm working on my first significant Python project and I'm having trouble with scope issues and executing code in included files. Previously my experience is with PHP.
What I would like to do is have one single file that sets up a number of configuration variables, which would then be used throughout the code. Also, I want to make certain functions and classes available globally. For example, the main file would i...
python - Put bar at the end of every line that includes foo
I have a list with a large number of lines, each taking the subject-verb-object form, eg:
Jane likes Fred
Chris dislikes Joe
Nate knows Jill
To plot a network graph that expresses the different relationships between the nodes in directed color-coded edges, I will need to replace the verb with an arrow and place a color code at the end of each line, thus, somewhat simplified:
Jane -> Fred r...
python - How to check if phone number entered by user includes country code?
Is there an easy way to check whether a phone number entered by the user includes country code and to validate that the number is correct? I don't use any specific formats, the number itself must be only digits, no ('s, -'s and the like. Is such validation possible without asking user for a country? The trick is that I want to work with all numbers world-wide.
I guess it can't be done with regex (googled a bit and...
python - PyCUDA: C/C++ includes?
Something that isn't really mentioned anywhere (at least that I can see) is what library functions are exposed to inline CUDA kernels.
Specifically I'm doing small / stupid matrix multiplications that don't deserve to be individually offloaded to the GPU but am offloading a larger section of the algorithm which includes this multiplication. Noone ever liked using their own linalg functions since someone has always ...
python - Something wrong without any error - Includes Tkinter
I'm not getting any error but the code doesn't do what I want so there must be somewhere in the code where I have made a mistake. What I want to do is if the words match then the words must be a pair and the two chosen cells should remain "self.hidden = False" and therefore the cells should still show the words behind the two cells. Else if the words doesn't match then the cells should be "self.hidden = True" and the two c...
python - Global includes in Django
I want to create a module containing different utility functions and classes to use across different apps. It's not going to define any models or views. What's the best way to do this?
python - How do I return a string that includes new lines?
I have a question that requires I use return and I do not know how to return on multiple lines. I need to be able to get an output that looks like this
Dear so and so,
kjhagjkahgsdhgl;dslhglk
jkasdhgjkdshkglhds;g
kjdghksadjglkdjslkg
kjgahkjsdhlgkdsjg;lsd
where the gibberish are strings that I have
python - Celery beat queue includes obsolete tasks
I'm using periodic celery tasks with Django. I used to have the following task in my app/tasks.py file:
@periodic_task(run_every=timedelta(minutes=2))
def stuff():
...
But now this task has been removed from my app/tasks.py file. However, I keep seeing call to this task in my celery logs:
[2013-05-21 07:08:37,963: ERROR/MainProcess] Received unregistered task of type u'ap...
Python: mplot3d, plot a 3D solid shape that includes dots inside
I am trying to plot in python using mplot3d, a solid shape that includes inside, a group of dots that are represented in a 3d space. Perhaps the images will clarify my question.
I was thinking of a sphere but also an irregular solid could work. If it is a sphe...
Still can't find your answer? Check out these communities...
PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python