0

After setting up the All-Auth app, when user logs in, he gets redirected to: accounts/profile/ page which tells us that the view doesn't exist.

I'm trying to figure out what kind of thing to include there, and decided to allow the user to change their basic information.

I have a users app with a Teacher model, which is set up as follows:

class Teacher(models.Model):
    user = models.ForeignKey(User, unique=True)
    rate = models.CharField(max_length=200)
    availability = models.BooleanField(default=False)
    verified = models.BooleanField(default=False)

I want the accounts/profile/ page to show a form, containing this information. The user can edit these fields and also edit their Firstname, Lastname and Email, which belong to a different Model - User.

I can't seem to get started on this. When I created a detailed view for the profile page, I get an error saying:

No PK or SLUG provided

I want Django to change the current users info, not based on the primary key in the URL. Do I need a custom view? I've looked at [other solutions1 but they seem to be utilising the private key parameter.

What I need is a working view function, something similar to (not working):

def get_teacher_info(request):
    current_user = request.user
    teacher = get_object_or_404(Teacher, username=current_user.username)
    return render(request, 'account/profile.html', {
            'user':current_user,
            'teacher': teacher,
            'error_message': "The field is blank",
        })

and in the accounts/urls.py I've added:

url(r"^profile/$", views.get_teacher_info, name="account_profile"),

but when I make calls like {% teacher.rate %} in the html template, I get: Invalid block tag on line 5: 'teacher.rate'. Did you forget to register or load this tag?

Roma
  • 449
  • 6
  • 23
  • Did you set: LOGIN_REDIRECT_URL, ACCOUNT_SIGNUP_FORM_CLASS & ACCOUNT_ADAPTER in settings.py? – panjianom Aug 18 '17 at 08:57
  • And also please try: url(r"^profile/$", views.get_teacher_info, name="get_teacher_info"), – panjianom Aug 18 '17 at 08:59
  • I've not set the above, but I basically lifted the code from the default 'polls' app. And it works there without setting those variables. I tried the 2nd comment too, without success – Roma Aug 18 '17 at 14:00
  • try to define function get_absolute_url to return slug or PK or username on your model – panjianom Aug 24 '17 at 12:14

2 Answers2

0

The def get_teacher_info(request) function and the urls.py entry are working. Looks like the issue may be in the template. Use {{ instead of {% tags. So use {{ teacher.rate }} not {% teacher.rate %} in the template.

MikeL
  • 1
  • 1
0

Redirecting to /accounts/profile/ After the login is the default behavior of Django. django-allauth is using the same default behavior from Django settings.

Now if you want to modify this default redirect url then change it to

LOGIN_REDIRECT_URL = '/custom/redirect-url'

Important Note: Without a starting / you're redirecting to a path that is appended to the CURRENT URL. You need to use a leading slash to redirect to a path that is appended to the domain root. further details on it. In short, without a starting / you will end up with

../login/callback/custom.redirect-url (appended to current url)

Umar Hayat
  • 4,300
  • 1
  • 12
  • 27