Django的;不会提出正确的要求

时间:2013-12-02 21:15:47

标签: python django django-templates request django-views

我刚开始学习如何使用django。我已经设置了views.py,urls.py,settings.py和相关的HTML页面。我能够得到索引页面但不是关于页面(只输出一些文本)或我的类别页面。我假设问题影响了他们两个。

Views.py:

from django.http import HttpResponse
from django.template import RequestContext
from django.shortcuts import render_to_response
from rango.models import Category
from rango.models import Page

def index(request):
    # Obtain the context from the HTTP request.
    context = RequestContext(request)

    category_list = Category.objects.order_by('id')[:5]
    context_dict = {'categories': category_list}

    # Render the response and send it back!
    return render_to_response('index.html', context_dict, context)

def about(request):
    # Request the context of the request.
    # The context contains information such as the client's machine details, for example.
    context = RequestContext(request)

    context_dict = {'boldmessage': "I am from the context"}

    return render_to_response('/about.html', context_dict, context)

def category(request, category_name_url):
    # Request our context from the request passed to us.
    context = RequestContext(request)

    category_name = category_name_url.replace('_', ' ')


    context_dict = {'category_name': category_name}

    try:

        category = Category.objects.get(name=category_name)

        pages = Page.objects.filter(category=category)

        # Adds our results list to the template context under name pages.
        context_dict['pages'] = pages

        context_dict['category'] = category
    except Category.DoesNotExist:


        # Go render the response and return it to the client.
    return render_to_response('rango/category.html', context_dict, context)

urls.py:

from django.conf.urls import patterns, url   
from rango import views  

# At the top of your urls.py file, add the following line:
from django.conf import settings

urlpatterns = patterns('', 
    url(r'$', views.index,name='index'),
    url(r'about/$', views.about,name='about')) 
    #url(r'category/$', views.category,name='category'))

# UNDERNEATH your urlpatterns definition, add the following two lines:
if settings.DEBUG:
    urlpatterns += patterns(
        'django.views.static',
        (r'media/(?P<path>.*)','serve',{'document_root': settings.MEDIA_ROOT}), )

我的模板目录已经过硬编码,所以它确实应该不是问题

TEMPLATE_DIRS = ('C:/Users/aharon/Desktop/TEMP',)

请记住,我是非常的菜鸟,所以请对我很轻松,我想尽可能多地解释。谢谢!

1 个答案:

答案 0 :(得分:1)

您尚未在开始时使用^锚定您的正则表达式。没有它,您的第一个模式将匹配每个URL。您应该确保它们都以^开头。