Sending data from Django view to Django form - python

I have a Django view and I want to send the request data to a form.
class PostDetailView(DetailView):
model = Post
form = CommentForm
def get_form_kwargs(self):
kwargs = super(PostDetailView, self).get_form_kwargs()
kwargs['user'] = self.request.user.username
return kwargs
def post(self, request, *args, **kwargs):
form = CommentForm(request.POST)
if form.is_valid():
post = self.get_object()
form.instance.user = request.user
form.instance.post = post
form.save()
return redirect(reverse("post-detail", args=[post.pk]))
from django import forms
from .models import Comment
from djrichtextfield.widgets import RichTextWidget
class CommentForm(forms.ModelForm):
user = forms.ChoiceField(required=True)
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
super(CommentForm, self).__init__(*args, **kwargs)
self.fields['user'].choices = self.user
content = forms.CharField(widget=RichTextWidget(attrs={
'class': 'md-textarea form-control',
'placeholder': 'Comment here ...',
'rows': '4',
}))
class Meta:
model = Comment
fields = ('author', 'content',)
<form method="POST" href="{% url 'post-detail' post.id %}">
{% csrf_token %}
<div class="form-group">
{{form | safe}}
</div>
<button class="btn btn-primary btn-block" type="submit">Comment</button>
</form>
urlpatterns = [
path('post/<int:pk>/', PostDetailView.as_view(), name="post-detail"),
]
According to the docs, get_form_kwargs allows key-value pairs to be set in kwargs. The kwargs is then passed into the form. The form's init function should then be able to pick up the user value from kwargs.
However, self.user returns None, and debugging showed that get_form_kwargs did not run at all.
I have two questions: how do functions in view classes get executed? And what is the correct method to pass data from a view to a form?
EDIT
I have refactored the comment feature into another view.
class AddCommentView(UpdateView):
model = Post
form = CommentForm
def post(self, request, *args, **kwargs):
form = CommentForm(request.POST)
if form.is_valid():
post = self.get_object()
form.instance.user = request.user
form.instance.post = post
form.save()
return redirect(reverse("post-detail", args=[post.pk]))
def get_form_kwargs(self):
kwargs = super(PostDetailView, self).get_form_kwargs()
kwargs.pop('instance', None)
kwargs['user'] = self.request.user.username
return kwargs
<form method="POST" href="{% url 'add-comment' post.id %}">
{% csrf_token %}
<div class="form-group">
{{form | safe}}
</div>
<button class="btn btn-primary btn-block" type="submit">Comment</button>
</form>
urlpatterns = [
path('post/<int:pk>/', AddCommentView.as_view(), name="add-comment")
]
However UpdateView cannot handle POST requests (405).

It's not explicitly described in the docs but get_form_kwargs is only triggered with a CreateView or an UpdateView.
In your case you can use UpdateView, and then use form_valid to do your post process. But note that we need to delete kwargs['instance'], because by default this view will think we are working with a Post object when in fact it's a Comment:
Try with this:
class PostDetailView(UpdateView):
model = Post
form = CommentForm
def get_form_kwargs(self):
kwargs = super(PostDetailView, self).get_form_kwargs()
# Remove the post object as instance,
# since we are working with a comment
kwargs.pop('instance', None)
kwargs['user'] = self.request.user.username
return kwargs
def form_valid(self, form):
instance = form.save(commit=False)
instance.user = self.request.user
instance.post = self.get_object() # returns the post
instance.save()
return redirect(reverse("post-detail", args=[post.pk]))
or if you don't want to use UpdateView (not recommended), you can just explicitly call get_form_kwargs when you build your form. You cannot call super().get_form_kwargs() though, since as discussed the parent class doesn't have this method:
class PostDetailView(DetailView):
model = Post
form = CommentForm
def get_form_kwargs(self):
kwargs = {'user': self.request.user.username}
return kwargs
def post(self, request, *args, **kwargs):
form = CommentForm(request.POST, **self.get_form_kwargs())
if form.is_valid():
post = self.get_object()
form.instance.user = request.user
form.instance.post = post
form.save()
return redirect(reverse("post-detail", args=[post.pk]))
Lastly, do consider renaming this class, since we are working with adding a comment to post, and not really about "post detail", so something like PostAddCommentView?

Related

Adding Google's reCAPTCHA a to a class-based view in Django

I want to add recaptcha for signup view in my Django app. This below uses decorators.py to achieve that. I have tried other tutorials for adding reCAPTCHA also but does not seem working. Any idea why?
views.py
class signup_view(generic.CreateView):
form_class = RegisterForm
template_name = 'users/signup.html'
success_url = reverse_lazy('users:login')
def form_valid(self, form):
if self.request.recaptcha_is_valid:
form.save()
return render(self.request, 'users/login.html', self.get_context_data())
return render(self.request, 'users/signup.html', self.get_context_data())
urls.py
path("signup", check_recaptcha(signup_view.as_view()), name="signup"),
decorators.py
from django.conf import settings
from django.contrib import messages
import requests
def check_recaptcha(function):
def wrap(request, *args, **kwargs):
request.recaptcha_is_valid = None
if request.method == 'POST':
recaptcha_response = request.POST.get('g-recaptcha-response')
data = {
'secret': settings.GOOGLE_RECAPTCHA_SECRET_KEY,
'response': recaptcha_response
}
r = requests.post('https://www.google.com/recaptcha/api/siteverify', data=data)
result = r.json()
if result['success']:
request.recaptcha_is_valid = True
else:
request.recaptcha_is_valid = False
messages.error(request, 'Invalid reCAPTCHA. Please try again.')
return function(request, *args, **kwargs)
wrap.__doc__ = function.__doc__
wrap.__name__ = function.__name__
return wrap
signup.html
<div class="form">
<form method="POST">
{% csrf_token %}
{{ form|crispy }}
<br>
<script src='https://www.google.com/recaptcha/api.js'></script>
<div class="g-recaptcha" data-sitekey="6LfzEg8gAAAAABcVpBvOjuLjs787K8_4Fu0N2wgu"></div>
<input type="submit" value="Sign Up">
</form>
</div>
Change your decorator to:
def wrap(request, *args, **kwargs):
request.recaptcha_is_valid = None
def wrap(obj, *args, **kwargs):
request = obj.request
request.recaptcha_is_valid = None
....
return function(obj, *args, **kwargs)
so it can works with django views.
In view put it before form_valid:
#check_recaptcha
def form_valid(self, form):

Django - Initial form field values not showing

I am trying to set the initial value of a form field.
I have approached it the following ways:
forms.py:
class FilterGigsForm(forms.Form):
field = forms.IntegerField(label="", required=False, initial=5)
def __init__(self, *args, **kwargs):
instance = kwargs.get('instance', None)
kwargs.update(initial={
'field': 5,
})
super().__init__(*args, **kwargs)
self.fields['field'].initial = 5
views.py
class HomePage(FormMixin, TemplateView):
template_name = 'index.html'
model = MyModel
form_class = MyForm
def get(self, request, *args, **kwargs):
form = MyForm(self.request.GET, initial={'field': 5,})
...
return render(request, 'index.html', {"form": form, })
I have loaded the form field into my template as follows:
<div class="styling">
{{ form.field|as_crispy_field }}
</div>
However, none of these approaches are working and the initial value is not showing when I load/reload the page.
form = MyForm(initial={'field': 5,})

Combine independent form with DetailView

I want to display DetailView and independent form to send API request to the other website server. I made views.py but only i get is empty page. I'm trying to figure out how to adjust it for over past fiew days and still don't have any clue how to do this. Hope you will help me with this
views.py
class DetailPostDisplay(DetailView):
model = EveryPost
template_name = 'post/detailpost.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['form'] = DictForm()
return context
class DictWindowForm(SingleObjectMixin, FormView):
template_name = 'post/detailpost.html'
form_class = DictForm
model = EveryPost
def post(self, request, *args, **kwargs):
self.object = self.get_object()
return super().post(request, *args, **kwargs)
def get_success_url(self):
return reverse('detailpost', kwargs={'slug': self.object.slug})
class DetailPostList(View):
def get(self, request, *args, **kwargs):
view = DetailPostDisplay.as_view()
return view(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
view = DictWindowForm.as_view()
return view(request, *args, **kwargs)
HTML
I'm not sure whether action should be empty or include url DetailPostDisplay(require to pass slug, which i don't have how to get)
<form method="POST" action="">
{% csrf_token %}
{{ form }}
<input type="submit" class="btn btn-dark float-right mt-2" value="Tłumacz">
</form>
urls.py
from django.urls import path
from . import views
from .views import PostListPl, PostListRu, DetailPostDisplay
urlpatterns = [
path('', PostListPl.as_view(), name='index_pl'),
path('ru/', PostListRu.as_view(), name='index_ru'),
path('about/', views.about, name='about'),
path('<slug:slug>/', DetailPostDisplay.as_view(), name='detailpost'),
]
For the future generations, i mixed and overthinked it. If you want to just put form into DetailView, create def post and put logic there. Code below:
views.py
class DetailPostDisplay(DetailView):
model = EveryPost
template_name = 'post/detailpost.html'
def get_context_data(self, **kwargs):
context = super(DetailPostDisplay, self).get_context_data(**kwargs)
context['form'] = DictForm
return context
def post(self, request, *args, **kwargs):
form = DictForm(request.POST)
if form.is_valid():
self.object = self.get_object()
And later code to pass variables into template from form
context = super(DetailPostDisplay, self).get_context_data(**kwargs)
context['form'] = DictForm
context['word'] = request.POST.get('word')
return self.render_to_response(context=context)

Error: Method Not Allowed (POST): "POST / HTTP/1.1" 405 0

I'm trying to make register possible on the homepage, so I don't have a seperate URL to handle registration. I'm trying to send the form through get_context_data, however it's not working. Here's my code:
forms.py
class UserRegistrationForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput)
class Meta:
model = User
fields = [
'username',
'password',
]
views.py
class BoxesView(ListView):
template_name = 'polls.html'
def get_context_data(self):
context = super(BoxesView, self).get_context_data()
# login
if self.request.method == 'POST':
form = UserRegistrationForm(self.request.POST or None)
context['form'] = form
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = User.objects.create_user(username=username, password=password)
user.save()
return redirect('/')
else:
print(form.errors) #doesn't print anything
print(form.non_field_errors()) #doesn't print anything
print('Errors') #doesn't print anything
else:
form = UserRegistrationForm()
context['form'] = form
return context
def get_queryset(self):
pass
base.html
<form action="" enctype="multipart/form-data" method="post">{% csrf_token %}
<div class="registerBox">
{{ form.username }}
{{ form.password }}
<input type="submit" value="register"/>
</div>
</form>
So when I submit the form it gives this error: Method Not Allowed (POST): "POST / HTTP/1.1" 405 0
And it isn't creating a new User. Any idea what the problem is?
EDIT: Tried FormMixin, got this error: The view app.views.BoxesView didn't return an HttpResponse object. It returned None instead.
class BoxesView(ListView):
template_name = 'polls.html'
form_class = UserRegistrationForm
def post(self, request, *args, **kwargs):
form = self.get_form()
if form.is_valid():
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = User.objects.create_user(username=username, password=password)
user.save()
return redirect('/')
def get_context_data(self):
context = super(BoxesView, self).get_context_data()
context['form'] = self.get_form()
return context
def get_queryset(self):
pass
Ok I see the issue fix the indentation, your if statement should be inside the get_context_data function not outside ;)
You need to add post() method and FormMixin to your CBV like this:
class BoxesView(FormMixin, ListView):
template_name = 'polls.html'
form_class = UserRegistrationForm
# ...
def post(self, request, *args, **kwargs):
form = self.get_form()
if form.is_valid():
# ...
else:
# ...
return render(request, self.template_name, {'data': some_data})

How can I get initial data in CreateView, Django

I use Django class-based views. And I have two classes: one for displaying form in the page, and second for handling it:
views.py:
class CommentFormView(CreateView):
form_class = AddCommentForm
model = Comment
success_url = '/'
def form_valid(self, form):
form.instance.author = self.request.user
form.instance.post = ????
return super(CommentFormView, self).form_valid(form)
class BlogFullPostView(BlogBaseView, DetailView):
model = Post
template_name = 'full_post.html'
pk_url_kwarg = 'post_id'
context_object_name = 'post'
def get_context_data(self, **kwargs):
context = super(BlogFullPostView, self).get_context_data(**kwargs)
context['form'] = AddCommentForm(initial={'post': self.object})
return context
full_post.html:
<form action="/addcomment/" method="post" >
{% csrf_token %}
{{ form }}
<button type="submit" >Add comment</button>
</form>
urls:
url(r'^blog/post/(?P<post_id>\d+)/$', BlogFullPostView.as_view()),
url(r'^addcomment/$', CommentFormView.as_view()),
And in def form_valid I need to fill field post, which value I have passed in BlogFullPostView in get_context_data: initial={'post': self.object}
But how can I get it in CommentFormView?
I have solved it in this way: Firstly, I try to use get_initial method. But I it does not return anything. So, I decide to hide auto filled field post - I make it as hidden field. So, then CreateView easily can create Comment object:
widgets = {
'content': forms.Textarea(),
'post': forms.HiddenInput(),
}

Categories