一个网址中的两个视图

时间:2011-09-11 10:28:39

标签: python django

大家好,我在两个应用程序中有两个视图 我想在主url文件中获取它们 我怎么能这样做?

项目/ app_news / views.py

中的第一个视图
def article_index(request):
        return render_to_response('news/index.html', {
            'news_slide':  Article.objects.filter(status=1, statusslide=1)[:6],
            'section_list' : Section.objects.all(),
            'last_lib' : Library.objects.all()[:3],
},
            context_instance=RequestContext(request)
)

项目/ app_poll / views.py中的secend视图

def questionlast(request):
    try:
        question = Question.objects.order_by('id').reverse()[0]
    except ObjectDoesNotExist, e:
        raise Http404    
    if request.method == 'POST':
        try:
            last_choice_id = request.session[question.id]
            last_choice = Choice.objects.get(id = last_choice_id)
            last_choice.total_votes -= 1
            last_choice.save()
        except KeyError, e:
            pass        
        choice_id = int(request.POST['choices'])
        choice = Choice.objects.get(id = choice_id)
        choice.total_votes += 1
        choice.save()
        request.session[question.id] = choice.id
        return HttpResponseRedirect(question.get_results_url())
    if request.method == 'GET':
        try:
            last_choice_id = request.session[question.id]
            last_choice = Choice.objects.get(id = last_choice_id)
        except KeyError, e:
            last_choice = 0
        choices = Choice.objects.filter(question = question)
        payload = {'question':question, 'choices':choices, 'last_choice':last_choice}

        return render('news/index.html', payload, request)

1 个答案:

答案 0 :(得分:-1)

urlpatterns = pattern('',
    url(r"^$", "app_news.views.article_index", name="index"),
    url(r"^questions/last/", "app_poll.views.questionlast", name="question-last"),
)