I started learning Django and got stuck. I am facing this error and I am at a loss as to what the issue is. The view on which this issue occurs (UserFormView
) is already returning a dictionary.
Error Log:
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
Here is my views code:
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})
Any help would be appreciated. Thank you.
You missed the colon after form
Try this:
return render(request, self.template_name, {"form": form})