Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django creating object from POST

I have a question regarding Djanog views

here is a sample code

def example register ( request ) :
if request.method == ’POST ’ :
username = request.POST.get ( ’ username ’ )
password = request.POST.get ( ’ password ’ )
email = request.POST.get (’email’)
user = User.objects .create_user ( username
, email
, password )
user . save ()
return HttpResponseRedirect (
’/ example /login / ’)

In the above example we are taking the values one by one i.e username, password etc. If I have many such fields, then how can I do it in one single line, i was thinking to use dict's but can not find a way. Any help is appreciated. Thank you.

like image 616
pratz Avatar asked Feb 11 '26 20:02

pratz


1 Answers

you should be using forms[1] and model-forms [2] to collect such data from the request.

[1] http://docs.djangoproject.com/en/dev/topics/forms/

[2] http://docs.djangoproject.com/en/dev/topics/forms/modelforms/

like image 157
Ashok Avatar answered Feb 13 '26 10:02

Ashok



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!