#future changing model
36 messages · Page 1 of 1 (latest)
what I did was that when you create your user in the registration form, then it links you to the setup page where you can assign default values like Temperature or Depth of the river
then when you click "Save" it saves the data into a model
but the thing is that it creates the model but that model isnt linked to the user that created it
Then you didn't code it in. It's usually done in the view after validating the form
because form by default doesn't have access to request and user
def setup_form(request):
if request.method == 'POST':
q = request.POST['q']
masl = request.POST['masl']
depth = request.POST['depth']
t = request.POST['temp']
data = user_data(q=q, masl=masl, depth=depth, t=t)
data.save()
return redirect('/')
else:
print('err')
return render(request, 'setup.html', {})
thats the code for the setup form
(that else is temporal)
using request.POST[key] is almost always a very bad pattern
var names also do not help understanding what is happening
what is user_data? Looks like a function, since it's lower cased
model is a class, classes should use PascalCase
oh ok
it's just hard to follow where you have an object or where is a Class without it
anyway, start with form class tutorial I've linked
ok
I don't remember if it shows setting user, if it doesn't I'll do it when you fix your view accordingly
the concept is correct but you don't validate the data, that might hurt in future really hard
and you probably just need a simple ModelForm to do it right
👍
@forest quarryone more thing, do you recommend me to do the same in the login and registration forms?
bc I have the same structure in those forms
I mean to change the registration and login form to that structure that you linked me
def login_page(request):
if request.method == "POST":
username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
return redirect('./')
else:
messages.success(request, ('Wrong Username or Password!, try again with another credentials.'))
return redirect('./login')
else:
return render(request, 'login.html', {})
I recommend not writing auth views at all for a start
there is built-in auth system and third-party extensions
im using the django auth system
but yes, every input, even not from user <form> should be validated before saving
on lower level. You can just include built-in views urls and be done
or extend built-in views if you need customization to it