Django:auth.Group下的配置文件,其中auth.Group用作应用程序模型中的外键

时间:2015-02-06 03:39:10

标签: django django-models django-templates django-users

我想显示登录用户组下的仪表板,我已经登录了该用户的用户信息和组名 到目前为止我的views.py是



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': request.user, 'groups': request.user.groups.all()}
    return render_to_response('registration/dashboards.html', context, context_instance=RequestContext(request))




dashboard.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>
       and you belongs to <strong>{{ group.name }}<strong> -
                {% endfor %}
      </li>
</ul>
{% endblock %}
&#13;
&#13;
&#13;

forms.py看起来像

&#13;
&#13;
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))
		
&#13;
&#13;
&#13;

和我的models.py是

&#13;
&#13;
from django.db import models
from django.contrib.auth.models import Group, User
from django.utils import timezone
import datetime

class Dashboard(models.Model):
    d_name = models.CharField(max_length=200)
    d_description = models.CharField(max_length=200)
    d_url = models.CharField(max_length=200)
    d_status = models.CharField(max_length=200)
    owner = models.ForeignKey(Group)
    
    def __str__(self):return self.d_name
&#13;
&#13;
&#13;

和urls.py是

&#13;
&#13;
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'^dashboards/$', 'mhawk.views.user'),
)
&#13;
&#13;
&#13;

因为我能够显示登录的用户名,就像我想要显示他的组以及该组下的仪表板一样,谢谢。期待着寻求帮助。

1 个答案:

答案 0 :(得分:0)

你能不能做这样的事情:

def user(request):
    user_groups = request.user.groups.all()
    if request.user.is_anonymous():
        user_groups = []
    user_dashboards = Dashboard.objects.filter(owner__in=user_groups)

    context = {
        'user': request.user,
        'user_groups': user_groups,
        'user_dashboards': user_dashboards,
    }

    [...]

在你的模板中,你需要做这样的事情......

{% 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 %}

    <p></p>
    <p>And you belongs to: </p>
    <ul>
        {% for group in user_groups %}
            <li>
                <strong>{{ group.name }}<strong> -
            </li>
        {% endfor %}
    </ul>
{% endblock %}

了解有关Django模板的更多信息:

https://docs.djangoproject.com/en/1.7/ref/templates/builtins/#for