为什么我的模板没有从一个函数获取视图数据?

时间:2018-06-18 03:39:39

标签: django

我创建了一个简单的文件上传应用。一切都很好。然后我想添加简单的登录,现在我有问题。 这些是我的观点

def login(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            user = authenticate(username=cd['username'],password=cd['password'])
            if user is not None:
                if user is active:
                    login(request,user)
                    return HttpResponse('Authenticated successfully')
                else:
                    return HttpResponse('Disabled account')
            else:
                return HttpResponse('Invalid login')
    else:
        form=LoginForm()

    return render(request,'account/login.html',{'form': form})



def list(request):
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Document(docfile=request.FILES['docfile'])
            newdoc.save()

            # Redirect to the document list after POST
            return HttpResponseRedirect(reverse('list'))
    else:
        form = DocumentForm()  # A empty, unbound form

    # Load documents for the list page
    documents = Document.objects.all()

    # Render list page with the documents and the form
    return render(request,'list.html',{'documents': documents, 'form': form})

这是fileupload / urls

from django.conf.urls import url
from fileupload.views import list
from fileupload.views import login

urlpatterns = [
    url(r'^list/$', list, name='list'),
    url(r'^login/$', login, name='login'),
]

我的表格

class LoginForm(forms.Form):
    username = forms.CharField()
    password = forms.CharField(widget=forms.PasswordInput)

class DocumentForm(forms.Form):
    docfile = forms.FileField(label='Select a file')

当我去的时候 http://127.0.0.1:8000/fileupload/list/ 一切正常。 如果我试试 http://127.0.0.1:8000/login/ 我懂了 enter image description here

我很困惑,因为模板是从一个有趣而不是从另一个获取数据。 如何调试?

1 个答案:

答案 0 :(得分:1)

很明显,您在网址中列出并登录的信息应该在http://127.0.0.1:8000/fileupload/之后

尝试检查http://127.0.0.1:8000/fileupload/login/

阅读此文档https://docs.djangoproject.com/en/1.11/topics/http/urls/