#future changing model

36 messages · Page 1 of 1 (latest)

gilded cedar
#

It's important to get the terminology right with Django, you will need to create a Model instance per new User, not a new Model.

high bough
#

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

forest quarry
#

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

high bough
#
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)

forest quarry
#

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

high bough
#

nop, thats the model

#

the model saves that data

forest quarry
#

model is a class, classes should use PascalCase

high bough
#

oh ok

forest quarry
#

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

high bough
#

ok

forest quarry
#

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

high bough
#

👍

#

@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

forest quarry
#

elaborate please

#

you have user make some choice at registration and every login?

high bough
#

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', {})
forest quarry
#

I recommend not writing auth views at all for a start

#

there is built-in auth system and third-party extensions

high bough
#

im using the django auth system

forest quarry
#

but yes, every input, even not from user <form> should be validated before saving

forest quarry
#

or extend built-in views if you need customization to it