Django 基于组提供内容
Django serving content based on group
我有一个调查应用程序,它包含以下模型 类:调查、回应、问题和答案。我想做的是根据用户所属的组限制问题。
我已经通读了 Django 模板文档(以及其他文档),但它们没有解释如何执行此操作。
我的想法是为 "type"(用户)向模型 "Question" 添加一个字段。因此,例如,Type=1 问题可能对所有人可见,而 Type=2 问题对部分用户可见(由他们添加到的组指定)。
这是正确的方法吗?是否有任何项目在做类似的事情我可以看看(我没能找到)。任何帮助,将不胜感激。提前致谢。
编辑:查看代码
# -*- coding: utf-8 -*-
from __future__ import (
absolute_import, division, print_function, unicode_literals
)
from django.conf import settings
from django.shortcuts import get_object_or_404, redirect, render
from django.views.generic import View
from future import standard_library
from survey.forms import ResponseForm
from survey.models import Category, Survey
standard_library.install_aliases()
class SurveyDetail(View):
def get(self, request, *args, **kwargs):
survey = get_object_or_404(Survey, is_published=True, id=kwargs['id'])
if survey.template is not None and len(survey.template) > 4:
template_name = survey.template
else:
if survey.display_by_question:
template_name = 'survey/survey.html'
else:
template_name = 'survey/one_page_survey.html'
if survey.need_logged_user and not request.user.is_authenticated():
return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
categories = Category.objects.filter(survey=survey).order_by('order')
form = ResponseForm(survey=survey, user=request.user,
step=kwargs.get('step', 0))
context = {
'response_form': form,
'survey': survey,
'categories': categories,
}
return render(request, template_name, context)
def post(self, request, *args, **kwargs):
survey = get_object_or_404(Survey, is_published=True, id=kwargs['id'])
if survey.need_logged_user and not request.user.is_authenticated():
return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
categories = Category.objects.filter(survey=survey).order_by('order')
form = ResponseForm(request.POST, survey=survey, user=request.user,
step=kwargs.get('step', 0))
context = {'response_form': form, 'survey': survey,
'categories': categories}
if form.is_valid():
session_key = 'survey_%s' % (kwargs['id'],)
if session_key not in request.session:
request.session[session_key] = {}
for key, value in form.cleaned_data.items():
request.session[session_key][key] = value
request.session.modified = True
next_url = form.next_step_url()
response = None
if survey.display_by_question:
if not form.has_next_step():
save_form = ResponseForm(request.session[session_key],
survey=survey, user=request.user)
response = save_form.save()
else:
response = form.save()
if next_url is not None:
return redirect(next_url)
else:
del request.session[session_key]
if response is None:
return redirect('/')
else:
next_ = request.session.get('next', None)
if next_ is not None:
if 'next' in request.session:
del request.session['next']
return redirect(next_)
else:
return redirect('survey-confirmation',
uuid=response.interview_uuid)
if survey.template is not None and len(survey.template) > 4:
template_name = survey.template
else:
if survey.display_by_question:
template_name = 'survey/survey.html'
else:
template_name = 'survey/one_page_survey.html'
return render(request, template_name, context)
您不希望在只响应用户可以访问的问题的视图中编写查询。以及可以过滤掉无权访问的用户的问题详细信息权限。
我有一个调查应用程序,它包含以下模型 类:调查、回应、问题和答案。我想做的是根据用户所属的组限制问题。
我已经通读了 Django 模板文档(以及其他文档),但它们没有解释如何执行此操作。
我的想法是为 "type"(用户)向模型 "Question" 添加一个字段。因此,例如,Type=1 问题可能对所有人可见,而 Type=2 问题对部分用户可见(由他们添加到的组指定)。
这是正确的方法吗?是否有任何项目在做类似的事情我可以看看(我没能找到)。任何帮助,将不胜感激。提前致谢。
编辑:查看代码
# -*- coding: utf-8 -*-
from __future__ import (
absolute_import, division, print_function, unicode_literals
)
from django.conf import settings
from django.shortcuts import get_object_or_404, redirect, render
from django.views.generic import View
from future import standard_library
from survey.forms import ResponseForm
from survey.models import Category, Survey
standard_library.install_aliases()
class SurveyDetail(View):
def get(self, request, *args, **kwargs):
survey = get_object_or_404(Survey, is_published=True, id=kwargs['id'])
if survey.template is not None and len(survey.template) > 4:
template_name = survey.template
else:
if survey.display_by_question:
template_name = 'survey/survey.html'
else:
template_name = 'survey/one_page_survey.html'
if survey.need_logged_user and not request.user.is_authenticated():
return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
categories = Category.objects.filter(survey=survey).order_by('order')
form = ResponseForm(survey=survey, user=request.user,
step=kwargs.get('step', 0))
context = {
'response_form': form,
'survey': survey,
'categories': categories,
}
return render(request, template_name, context)
def post(self, request, *args, **kwargs):
survey = get_object_or_404(Survey, is_published=True, id=kwargs['id'])
if survey.need_logged_user and not request.user.is_authenticated():
return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))
categories = Category.objects.filter(survey=survey).order_by('order')
form = ResponseForm(request.POST, survey=survey, user=request.user,
step=kwargs.get('step', 0))
context = {'response_form': form, 'survey': survey,
'categories': categories}
if form.is_valid():
session_key = 'survey_%s' % (kwargs['id'],)
if session_key not in request.session:
request.session[session_key] = {}
for key, value in form.cleaned_data.items():
request.session[session_key][key] = value
request.session.modified = True
next_url = form.next_step_url()
response = None
if survey.display_by_question:
if not form.has_next_step():
save_form = ResponseForm(request.session[session_key],
survey=survey, user=request.user)
response = save_form.save()
else:
response = form.save()
if next_url is not None:
return redirect(next_url)
else:
del request.session[session_key]
if response is None:
return redirect('/')
else:
next_ = request.session.get('next', None)
if next_ is not None:
if 'next' in request.session:
del request.session['next']
return redirect(next_)
else:
return redirect('survey-confirmation',
uuid=response.interview_uuid)
if survey.template is not None and len(survey.template) > 4:
template_name = survey.template
else:
if survey.display_by_question:
template_name = 'survey/survey.html'
else:
template_name = 'survey/one_page_survey.html'
return render(request, template_name, context)
您不希望在只响应用户可以访问的问题的视图中编写查询。以及可以过滤掉无权访问的用户的问题详细信息权限。