上下文必须是字典而不是集合

时间:2019-02-07 13:35:12

标签: django django-models django-forms django-templates django-views

我已经开始学习Django,并且在这一点上遇到了麻烦。我正面临这个问题,而我对这个问题感到茫然。即使发生此问题的视图(即UserFormView)正在返回字典。

错误日志:

TypeError at /music/register/

context must be a dict rather than set.

Request Method:     POST
Request URL:    http://127.0.0.1:8000/music/register/
Django Version:     2.1.5
Exception Type:     TypeError
Exception Value:    

context must be a dict rather than set.

Exception Location:     E:\Python Projects\Django_Enviroment\lib\site-packages\django\template\context.py in make_context, line 270
Python Executable:  E:\Python Projects\Django_Enviroment\Scripts\python.exe
Python Version:     3.6.3
Python Path:    

['E:\\Python '
 'Projects\\Django_Enviroment\\Django_Projects\\Test_Projects\\ist_site',
 'E:\\Python Projects\\Django_Enviroment\\Scripts\\python36.zip',
 'E:\\Python 3.6.3\\DLLs',
 'E:\\Python 3.6.3\\lib',
 'E:\\Python 3.6.3',
 'E:\\Python Projects\\Django_Enviroment',
 'E:\\Python Projects\\Django_Enviroment\\lib\\site-packages']

Server time:    Thu, 7 Feb 2019 13:20:49 +0000

这是我的观看代码:

from django.views import generic
from .models import Album
from django.shortcuts import render,redirect
from django.contrib.auth import authenticate,login
from django.views import generic
from django.views.generic import View
from django.views.generic.edit import CreateView,UpdateView,DeleteView
from django.urls import reverse_lazy
from .forms import UserForm   

class IndexView(generic.ListView):
    template_name = "music/index.html"
    context_object_name='all_albums'

    def get_queryset(self):
            return Album.objects.all()

class DetailView(generic.DetailView):

    model=Album
    template_name='music/detail.html'

class AlbumCreate(CreateView):
    model=Album
    fields=['artist','album_title','genre','album_logo']

class AlbumUpdate(UpdateView):
    model=Album
    fields=['artist','album_title','genre','album_logo']

class AlbumDelete(DeleteView):
    model=Album
    success_url=reverse_lazy('music:index')

class UserFormView(View):
    form_class=UserForm
    template_name='music/registration_form.html'



  def get(self,request):
        form=self.form_class(None)
        return render(request,self.template_name,{'form':form})

    def post(self,request):
        form=self.form_class(request.POST)

        if form.is_valid():

            #clean data
            user=form.save(commit=False)
            username=form.cleaned_data['username']
            password=form.cleaned_data['password']
            user.set_password(password)
            user.save()


            #return user if the data entered is correct
            user=authenticate(username=username,password=password)
            if user is not None:
                if user.is_active:
                    login(request,user)
                    return redirect('music:index')

        return render(request,self.template_name,{'form',form})

任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

您在form后错过了冒号

尝试一下:

return render(request,self.template_name,{"form":form})