I'm doing registration on Django. I made a form, added a button that should refresh the page and send a POST request. But the button does not update the page and does not send the request. I doesn't have any scripts on JavaScript. My views.py code:
from django.shortcuts import render, redirect
from .models import User
from .forms import UserForm
from django.views import View
def index(request):
return render(request, 'index.html')
def inn(request):
return render(request, 'in.html')
class PostCreate(View):
def up(self, request):
form=UserForm(request.POST)
if form.is_valid():
form.save()
return redirect('home')
def down(request):
return render(request, 'up.html')`
My forms.py code:
from .models import User
from django.forms import ModelForm
class UserForm(ModelForm):
class Meta:
model=User
fields=['NickName', 'Email', 'Password']`
And HTML: HTML
I'm tried to search any information on internet, but I'm found only about event.preventDefault() and type='button'. But I doesn't have any scripts and my type is 'submit'
You did not write <form>
, but wrote instead, so therefore it does not act like a form at all, and thus the button does not submit.<field>
But that is not the only problem: you used .up(…)
and .down(…)
as methods in your view. A default view has a .get(…)
, .post(…)
and other HTTP methods as names. You can make the work even more convenient by using a CreateView
[Django-doc]:
from django.urls import reverse_lazy
from django.views.generic.edit import CreateView
class PostCreateView(CreateView):
form_class = UserForm
template_name = 'up.html'
success_url = reverse_lazy('home')
Note: In Django, class-based views (CBV) often have a
…View
suffix, to avoid a clash with the model names. Therefore you might consider renaming the view class toPostCreateView
, instead of.PostCreate