简体   繁体   中英

Displaying User Specific Information with Django

So I know this question is a bit broad, but I am working on a web application using Django and I have hit a snag; So I want to have user specific data (that is imputed by the user) stored in the users model. (I have completed the user model and it works as expected, and I am using a custom user class so that I can have the needed data fields.)

So the issue is I want to have one url for a page (ie. www.example.com/profile/) but have the information displayed to be specific to the user that is logged in. And have the user be able to change the info and have the database updated with the changes. I have read the Django documentation on sessions but I am unsure if it would work for my use case or exactly how I would implement it. Thanks for the help.

               Cheers

On django, be sure that if you log a user, if you use request.user , it will send you the user which is logged in.

From this you can easily get all the information you want and then displaying them into inputs on your template giving from a ModificationForm for example.

Then with this form you can request it in your view like you know with form = ModificationForm(request.POST) .

I don't know if I have answered your question well, tell me if you want more. I already did exactly what you want to do so I can help you.

I give you an example below

forms.py

class ModificationForm(forms.ModelForm):
  name = models.CharField()
  ...

views.py

def ModificationView(request):
  if request.user.is_authenticated():
    if request.method == 'POST':
      form = ModificationForm(request.POST)
      if form.is_valid():
        [ get all informations from inputs ]
    else:
      formModif = ModificationForm()
      profile = request.user
  else:
    HttpRedirect('404/')
  return render(request, 'profil/', locals())

profil.html

<html>
  ...
    <!-- Example of one input -->
    <input type="text" name="email" value="{{ profile.user.email }}"
  ...
</html>

I am not sure of all the fonction's name or how to get back the email for example because it's been a while I didn't touch Django but I think this can work with the good names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM