2

I am writing an simple django application and got stuck into this error, can some one please help me my views.py looks exactly as

def custom_login(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect('dashboards')        
    return login(request, 'login.html', authentication_form=LoginForm)

def custom_logout(request):
    return logout(request, next_page='/')

def user(request):
        context = {'user': user, 'groups': request.user.groups.all(), 'dashboards': Dashboard}
    return render_to_response('registration/dashboards.html', context, context_instance=RequestContext(request))

and my forms.py is like

from django import forms
from django.contrib.auth.models import User
from django.forms import ModelForm
#from mhawk.models import Dashboard


class LoginForm(forms.Form):
        username        = forms.CharField(label=(u'User Name'))
        password        = forms.CharField(label=(u'Password'), widget=forms.PasswordInput(render_value=False))

and the thing is I am trying to display the logged in username in an html page.

here is my trace back of error

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/user/

Django Version: 1.7.3
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'mhawk')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  204.                 response = middleware_method(request, response)
File "/usr/local/lib/python2.7/dist-packages/django/middleware/clickjacking.py" in process_response
  31.         if response.get('X-Frame-Options', None) is not None:

Exception Type: AttributeError at /user/
Exception Value: 'User' object has no attribute 'get'

urls.py looks like

from django.conf.urls import patterns, include, url
from django.contrib import admin
from drinker import views, models
from django.contrib.auth.models import User

urlpatterns = patterns('',

    url(r'^admin/', include(admin.site.urls)),
    url(r'^login/$', 'django.contrib.auth.views.login',),
    url(r'^logout/$', 'django.contrib.auth.views.logout'),
    url(r'^user/$', 'mhawk.views.User'),
)

dashboards.html:-

{% extends "base.html" %}
{% block content %}
{% if user.is_authenticated %}
    <p>Welcome, {{ request.user.get_username }}. <br/>
    {% else %}
    <p>Welcome, new user. Please log in.</p>
{% endif %}

<ul>
{% for group in groups %}
    <li>
        <strong>{{ group.name }}<strong> -
        
            {{ dashboards.d_name }}{% if not forloop.last %},{% endif %}
        
    </li>
{% endfor %}
</ul>



{% endblock %}
jay j
  • 285
  • 1
  • 6
  • 16

1 Answers1

2

Edit

There is your problem, in urls, you are pointing not to a view, but to a User. Python is case sensitive, you know. :)


Your problem is instead of response, middleware is getting a User object, so somwhere, instead of response you are returning a user, I don't see it in your code though.

Regardless, why don't you use django's authentication views? They do same stuff as you are trying to implement.

from django.contrib.auth.views import login, logout

def custom_login(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect('dashboards')        
    return login(request, 'login.html', authentication_form=LoginForm)

def custom_logout(request):
    return logout(request, next_page='/')

Oh yeah, and add this to your settings:

LOGIN_REDIRECT_URL = '/dashboards/'

And here is a promised user view:

from django.contrib.auth.decorators import login_required

@login_required
def user(request):
    # btw 'user' variable is already available in templates
    context = {'user': request.user}
    return render_to_response('dashboards.html', context, context_instance=RequestContext(request))
lehins
  • 9,642
  • 2
  • 35
  • 49
  • and should i take off user(request) in views.py – jay j Feb 05 '15 at 05:25
  • yeah you should, I'm typing up an edit, what you should do there too – lehins Feb 05 '15 at 05:27
  • So, from your code, here is the biggest suggestion I have for you. Only use first capital letter for classes, and not for methods or variables, it is a convention in practically all languages. Your problem was a just another proof of this. Otherwise, good luck in studying django. – lehins Feb 05 '15 at 05:39
  • my dashboards.html template is inside template/registration folder of my application where login and logout.html exists. but i am getting template does not exist error – jay j Feb 05 '15 at 06:02
  • what about the dashboards template? – lehins Feb 05 '15 at 06:03
  • Then use `registration/dashboards.html` instead. You see, you need to give a path to a template, relative to the app's `templates` directory, unless you add paths to `TEMPLATE_DIRS` in settings – lehins Feb 05 '15 at 06:07
  • hiii alex may i know how to get the users group name using the username – jay j Feb 05 '15 at 09:14
  • If you are talking about `django.contrib.auth.models.Group`'s, then you should be able to get all groups user belongs to using this query: `groups = user.groups.all()`, that's of course if you got the user, otherwise: `groups = Group.objects.filter(user__username='username')` – lehins Feb 05 '15 at 09:26
  • yeah its the django models.Group's, i want to display group name as i have done it previously for username hope u remember, thank you – jay j Feb 05 '15 at 09:28
  • `User` has ManyToMany relationship with `Group`, hence the way you can get groups for a user is explained in my previous comment. I gotta go to sleep, I'll try to answer any questions you have tomorrow. – lehins Feb 05 '15 at 09:35
  • alex if you are there, please see my question that i have posted today and please let me know what can be done, thank you. – jay j Feb 06 '15 at 04:06
  • hello can some one please help me how to fetch the dashboards which are having Group as Foreign key with the same model above.its very urgent please help me – jay j Feb 10 '15 at 05:26