Django无法显示网页

时间:2016-12-05 21:01:02

标签: python django

1尝试使用Django 1.10创建文件上传系统(类似于此示例here。我的主要问题是,无论我怎么努力,Django都无法显示我的网页( 404错误)。我不明白为什么。我按照1.9的例子进行操作,据我所知,它应该正常工作。我已经附加了错误和我的数据树

error message

[D:.
│   db.sqlite3
│   manage.py
│
├───.idea
│       courseworkupload.iml
│       misc.xml
│       modules.xml
│       workspace.xml
│
├───courseworkupload
│   │   settings.py
│   │   urls.py
│   │   wsgi.py
│   │   __init__.py
│   │
│   └───__pycache__
│           settings.cpython-35.pyc
│           urls.cpython-35.pyc
│           wsgi.cpython-35.pyc
│           __init__.cpython-35.pyc
│
├───upload
│   │   admin.py
│   │   apps.py
│   │   forms.py
│   │   models.py
│   │   tests.py
│   │   urls.py
│   │   views.py
│   │   __init__.py
│   │
│   ├───migrations
│   │   │   0001_initial.py
│   │   │   __init__.py
│   │   │
│   │   └───__pycache__
│   │           0001_initial.cpython-35.pyc
│   │           __init__.cpython-35.pyc
│   │
│   ├───templates
│   │       Final.html
│   │       upload.html
│   │
│   ├───uploadedfiles
│   └───__pycache__
│           admin.cpython-35.pyc
│           forms.cpython-35.pyc
│           models.cpython-35.pyc
│           urls.cpython-35.pyc
│           views.cpython-35.pyc
│           __init__.cpython-35.pyc
│
├───Uploadedfiles
└───__pycache__
        manage.cpython-35.pyc][2]

Views.py

from django.http import HttpResponseRedirect
from django.http import HttpResponse
from django.shortcuts import render
from django.core.urlresolvers import reverse
from .forms import docfieldform
from .models import Document



def upload(request):
    if request.method == 'POST':
        form = docfieldForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Document (docfile=request.FILES['newfile'])
            newdoc.save()

            # Redirect to the document list after POST
            return HttpResponseRedirect(reverse('upload'))
    else:
        form = docfieldform()

    return render( request,'Final.html',)

1 个答案:

答案 0 :(得分:1)

您需要删除网址中的.html,使其成为url(r'^upload/$', upload, name='upload')。因此,如果您的浏览器网址为http://127.0.0.1:8000/upload/upload/,则应转到upload视图。

在上传视图中以upload.html替换render(request, 'Final.html')render(request, 'upload.html')显示内容