0

I am currently using the django auth_views.login to log my users into their accounts.

I need to pass a user's attributes such as pk/username/etc. to my user profile view.

my profile url is:

url(r'^(?P<pk>[\w.@+-]+)$', ProfileDashboardView , name='dashboard')

Once the user is logged in, how would I get their user attributes such as pk/id/username/etc. to pass into the url?

I am using TDD to test before actually moving forward with my view.

This is my view so far:

@login_required
def ProfileDashboardView(request, name=None):
    usr_name = IbkUser.objects.get(name=name)
    return render(request, 'profiles/profile_dashboard.html')

I am using an AbstractBaseUser model for my user accounts that extends the USER model and uses the email as the username for login purposes.

I am also using AuthenticationForm for the login form and crispy forms for styling.

my form:

class LoginAuthenticationForm(AuthenticationForm):
    """
    A user login form for registered users.
    """
    username = forms.EmailField(label='Email', required=True,   widget=forms.TextInput(attrs={'id': 'login_username'}))
    password = forms.CharField(label='Password', required=True,widget=forms.PasswordInput(attrs={'id': 'login_password'}))

    def __init__(self, *args, **kwargs):
         super(LoginAuthenticationForm, self).__init__(*args, **kwargs)
         self.helper = FormHelper()
         self.helper.form_id = 'loginForm'
         self.helper.form_method = 'post'
         self.helper.layout = Layout(
                PrependedText('username', "<span class='glyphicon glyphicon-envelope'></span>", active=True),
                PrependedText('password', "<span class='glyphicon glyphicon-lock'></span>", active=True),
                FormActions(
                     Submit('submit', 'Submit', css_class ='btn btn-success btn-lg btn-block'),
                    ),
             )

and this is my login template:

{% extends "auths/base.html" %}
{%load staticfiles%}
{% load crispy_forms_tags%}

{%block auths_title%}Login{%endblock auths_title%}
{%block auths_css%}{%endblock auths_css%}

{%block auths_body%}
    <div class="row" id="signup-container">
        <div class="col-md-4"></div>
        <div class="col-md-4">
            <div class="page-header text-info text-center">
                <h2>Login</h2>
            </div>
            {% crispy form form.helper %}
            <p class="text-center">
                Forgot your password? <a href="{% url 'auths:recover'%}">Reset Password</a>
            </p>
            <p class="text-center">
                <small>Don't have an account ? <a href="{% url 'accounts:register' %}">Sign up</a></small>
            </p>
            <div class="col-md-4"></div>
        </div>
    </div>
{%endblock auths_body%}

or should I just do my own custom view? lol

Corey Gumbs
  • 372
  • 6
  • 13

1 Answers1

1

What you need is a redirect...

from django.shortcuts import redirect
from django.core.urlresolvers import reverse_lazy

def my_login(request)
    # your login stuff...
    return redirect(reverse_lazy('dashboard',kwargs={'name':request.user.username}))
Juan D. Gómez
  • 479
  • 1
  • 7
  • 21
  • 1
    I think this should return an error `NonType has no attribute username` in your `request.user.username` if their is Anonymous user... – binpy Feb 03 '17 at 01:47
  • 1
    Well, this code should be executed just after the user logged in. I supossed that you first log in your user and then you redirect him to the dashboard – Juan D. Gómez Feb 03 '17 at 01:49
  • Im using the django auth_views.login, so basically I should just make my own log in view rather than the one provided by django? – Corey Gumbs Feb 03 '17 at 02:00
  • 1
    You can use the LOGIN_REDIRECT_URL to redirect the user after login, however you cannot pass params, so... yeah, maybe is better that you do your own login view. If you want I can post an example. – Juan D. Gómez Feb 03 '17 at 02:05
  • @JuanD.Gómez thank you. I was suspecting that but was hoping I didn't have to. I appreciate your help. – Corey Gumbs Feb 03 '17 at 02:07
  • 1
    Check this link if you need more info https://docs.djangoproject.com/en/dev/topics/auth/default/#how-to-log-a-user-in – Juan D. Gómez Feb 03 '17 at 02:08
  • 1
    By the way, I'm glad to help, and if you can accept my answer, it would be great. – Juan D. Gómez Feb 03 '17 at 02:08
  • and also this one, if you use the `email` as `username` http://stackoverflow.com/a/37332393/6396981 – binpy Feb 03 '17 at 02:10