Django url的问题

时间:2011-05-03 01:47:18

标签: django django-urls

转到http://127.0.0.1:8300/projects/cprshelp/edit_file/?filename=manage.py会导致404错误

urls.py

(r'^projects/(?P<project_name>[\w ,-<>]+)/', include('projects.urls')),

项目/ urls.py

(r'edit_file/$', views.edit_file),

我需要更改为我的网址文件以使此特定网址有效吗?

有效网址列表:

^admin/doc/
^admin/
^projects/(?P<project_name>[\w ,-<>]+)/ edit_file/$
^media/(?P<path>.*)$

edit_file函数:

def edit_file(request, project_name):
    print '**** project name ' + project_name
    #project = Project.objects.get(name=project_name)

    filename = request.GET['filename']
    #content_of_file = open(project.file_location + filename, 'r')
    #content_of_file = '\n'.join(content_of_file.readlines())

    context = RequestContext(request, {
        #"project": project,
        #"files": get_files_and_directories(project.file_location),
        "filename": filename,
        #"content_of_file": content_of_file,
    })        

    return render_to_response("edit_file.html", context)

2 个答案:

答案 0 :(得分:1)

您需要在正则表达式中反斜杠-

>>> p = re.compile(r'projects/[\w ,-<>]+/')>>> p.search('http://127.0.0.1:8300/projects/cprshelp/edit_file/?filename=manage.py').group()
'projects/cprshelp/edit_file/'
>>> p = re.compile(r'projects/[\w ,\-<>]+/')>>> p.search('http://127.0.0.1:8300/projects/cprshelp/edit_file/?filename=manage.py').group()
'projects/cprshelp/'
>>> 

还考虑使用.get

filename = request.GET.get('filename','')

答案 1 :(得分:0)

尝试将项目/ urls.py更改为

(r'^edit_file/$', views.edit_file),