Method Not Allowed (POST) in Django 3.0.7 - python

I'm new to Django. I 'm trying to practice create Raw Update Class Based View but it get in to error Method Not Allowed (POST). Can any one help me please.
I already follow the instruction step by step but the result is the same. I also try a lot of method to fix but still can't. I really want to know what is the problem. I very appreciate for someone help me fix this.
view.py
from django.shortcuts import render, get_object_or_404
from django.views import View
from .models import Client
from .forms import ClientFrom
class ClientUpdateView(View):
template_name = "clients/client-update.html"
def get_object(self):
id = self.kwargs.get('id')
obj = None
if id is not None:
obj = get_object_or_404(Client, id=id)
return obj
def get(self, request, id=None, *args, **kwargs):
#GET Method
context = {}
obj = self.get_object()
if obj is not None:
form = ClientFrom(instance=obj)
context['object'] = obj
context['form'] = form
return render(request, self.template_name, context)
def post(self, request, id=None, *args, **kwargs):
#POST Method
context = {}
obj = self.get_object()
if obj is not None:
form = ClientFrom(request.POST, instance=obj)
if form.is_valid():
form.save()
context['object'] = obj
context['form'] = form
return render(request, self.template_name, context)
forms.py
from django import forms
from .models import Client
class ClientFrom(forms.ModelForm):
class Meta:
model = Client
fields = [
'Name',
'Address',
'Contact',
]
form in client-update.html
{% extends 'clienthead.html' %}
{% block content %}
<h1>Update: {{object.id}} - {{ object.Name }}</h1>
<form action='.' method='POST'> {% csrf_token %}
{{ form.as_p }}
<input type='submit' value='Save' />
</form>
{% endblock %}
urls.py
from django.urls import path
from .views import (
ClientUpdateView,
)
app_name = 'client'
urlpatterns = [
path('<int:id>/adjust', ClientUpdateView.as_view(), name='client-adjust'),
]

Related

Comment not being added to the database

I am working on a movie website with Django and I'm trying to add user comments. Only the registered users should be allowed to comment. I have the following so far:
models.py:
class Comment(models.Model):
movie = models.ForeignKey(Movie, related_name = "comments", on_delete=models.CASCADE)
user = models.ForeignKey(User, on_delete = models.CASCADE)
content = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return '%s - %s' % (self.movie.title, self.name)
forms.py
class CommentForm(forms.ModelForm):
content = forms.CharField(label ="", widget = forms.Textarea(
attrs ={
'class':'form-control',
'placeholder':'Comment here!',
'rows':5,
'cols':50
}))
class Meta:
model = Comment
fields =['content']
views.py
class MovieDetailsView(generic.DetailView):
model = Movie
template_name = "details.html"
def comment(request, id):
movie= Movie.objects.get(id)
if request.method == 'POST':
cf = CommentForm(request.POST or None)
if cf.is_valid():
content = request.POST.get('content')
comment = Comment.objects.create(movie = movie, user = request.user, content = content)
comment.save()
return redirect(movie.get_absolute_url())
else:
cf = CommentForm()
context ={
'comment_form':cf,
}
return render(request, 'details.html', context)
details.html
<form method="POST" action="#" class="form">
{% csrf_token %}
{{comment_form.as_p}}
<button type="submit" class="btn btn-primary btn-lg">Submit</button>
<textarea id="text" name="text" class="form__textarea" placeholder="Add comment"></textarea>
<button type="button" class="form__btn">Send</button>
</form>
The MovieDetailsView displays the details page of the movie and has a comment section. However, when I submit the comment, it simply displays a white page and this link: http://127.0.0.1:8000/details/1# . The comment is not saved on the database and I can't seem to find what the issue is. I am also following the example from this link.
Thanks in advance!
You can combine FormMixin with DetailView - Using FormMixin with DetailView.
from django.urls import reverse
from django.views.generic.detail import DetailView
from django.views.generic.edit import FormMixin
from django.http import HttpResponseForbidden
class MovieDetailsView(FormMixin, DetailView):
model = Movie
template_name = "details.html"
form_class = CommentForm
def get_success_url(self):
return reverse('movie_detail', kwargs={'pk': self.object.pk})
# or
# return self.object.get_absolute_url()
def post(self, request, *args, **kwargs):
if not request.user.is_authenticated:
return HttpResponseForbidden()
self.object = self.get_object()
form = self.get_form()
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
def form_valid(self, form):
form.instance.user = self.request.user
form.instance.movie = self.object
form.save()
return super().form_valid(form)
In the post() method, we first check if the user is logged in:
if not request.user.is_authenticated:
return HttpResponseForbidden()
In template:
<form method="POST" class="form">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" class="btn btn-primary btn-lg">Submit</button>
</form>
You can remove the action attribute, the form will be submitted to the same page.

Django Select2MultipleWidget leads to form continuously trying to load

I'm trying to incorporate Select2 into my django form -- specifically ModelSelect2MultipleWidget -- so that the user can associate multiple event objects to another model (like CheckboxSelectMultiple). The associated models are:
from django.contrib.auth import get_user_model
from django.db import models
class F4Events(models.Model):
name = models.CharField(max_length=300)
handling_type = models.ManyToManyField(WaferHandlingType)
def __str__(self):
return self.name
class ToolPm(models.Model):
ceid = models.ForeignKey(CEID, on_delete=models.CASCADE)
name = models.CharField(max_length=250)
wafer_handling = models.BooleanField(default=True)
wafer_based = models.BooleanField(default=False)
frequency = models.FloatField(blank=True, null=True)
handling_type = models.ManyToManyField(WaferHandlingType, blank=True)
user_edited = models.BooleanField(default=False)
pm_f4_events = models.ManyToManyField('F4Events', blank=True)
def __str__(self):
return str(self.name) if self.name else ''
class Meta:
ordering = ('ceid', 'name')
My forms.py file is:
from django import forms
from .models import Entity, ToolPm, CEID, PDL, F4Events, WaferHandlingType
from django_select2 import forms as s2forms
class F4EventWidget(s2forms.ModelSelect2MultipleWidget):
search_fields = [
'name__icontains',
]
attrs = {'data-placeholder': 'Please Choose F4 Events'}
class PMForm(forms.ModelForm):
class Meta:
model = ToolPm
fields = ['wafer_handling', 'wafer_based', 'handling_type', 'pm_f4_events']
widgets = {'handling_type': forms.CheckboxSelectMultiple,
'pm_f4_events': F4EventWidget
}
my view.py is:
from datetime import datetime, timedelta
from django.db.models import Sum
from django.http import JsonResponse, HttpResponseRedirect
from django.http.response import HttpResponse
from django.shortcuts import render, get_object_or_404, redirect
from django.views import View
from django.views.generic import ListView
from django_filters.views import FilterView
from pages.filter import CeidFilter, PmRunDateFilter
from tools.forms import EntityForm, PMForm, CEIDForm, PDLAddForm
from tools.models import CEID, Entity, F4Events, ToolPm, WaferHandlingType, PmRunDate
from django.urls import reverse
class UserCeidPMUpdate(View):
template_name = 'tools/update_pm_attributes.html'
def get(self, request, username, pk, pm_id):
pm = ToolPm.objects.get(pk=pm_id)
form = PMForm(request.GET or None, instance=pm)
return render(request, self.template_name, {'form': form, 'pm': pm, })
def post(self, request, username, pk, pm_id):
pm = ToolPm.objects.get(pk=pm_id)
pm.user_edited = True
pm.save()
form = PMForm(request.POST or None, instance=pm)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('pm_view', kwargs={'username': username, 'pk': pm.ceid.id}))
return render(request, self.template_name, {'form': form, 'pm': pm, })
lastly, my .html file is:
{% extends 'base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<form method="post">
<p>Process: {{ pm.ceid.process }}</p>
<p>CEID: {{ pm.ceid.ceid }}</p>
<p>Name: {{ pm.name }}</p>
<p>Frequency: {{ pm.frequency }}{% if pm.wafer_based %} wafers {% else %} days {% endif %}</p>
<p>Score: {{ pm.score }}</p>
{% csrf_token %}
{{ form }}
<input type="submit" class="btn btn-success" value="Update PM"><button type="button" class="btn btn-primary">Go Back</button>
</form>
{% endblock content %}
If I switch the pm_f4_events widget to a CheckboxSelectMultiple or a ModelSelect2Widget, the code works.
However, when I try to use a ModelSelect2MultipleWidget, the form continually tries to load but cannot and eventually just times out.
There are ~5000 items within the F4Events model, so that may have something to do with it. Any help to point me in the right direction would be greatly appreciated!
You can empty the pm_f4_events field's choices in the __init__ method:
class PMForm(forms.ModelForm):
class Meta:
model = ToolPm
fields = ['wafer_handling', 'wafer_based', 'handling_type', 'pm_f4_events']
widgets = {
'handling_type': forms.CheckboxSelectMultiple,
'pm_f4_events': s2forms.ModelSelect2MultipleWidget(
model=F4Events,
search_fields=["name__icontains",],
attrs = {'data-placeholder': 'Please Choose F4 Events'}
)
}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
choices_F4Events = []
pm_f4_events_field = self.fields["pm_f4_events"]
# selected values
selected_F4Events = self.data.getlist("pm_f4_events") or self.initial.get("pm_f4_events")
if selected_F4Events:
# check of selected values not object
if not isinstance(selected_F4Events[0], F4Events):
selected_F4Events = pm_f4_events_field.queryset.filter(pk__in=selected_F4Events)
choices_F4Events = [(item.pk, item.name) for item in selected_F4Events]
# assign selected values or empty list
pm_f4_events_field.choices = choices_F4Events
Has been tested on my server, works well.

How to fix No Reverse Match error in Django

I am having this error any time i want to view posts that belong to a group
NoReverseMatch at /groups/posts/in/python/
Reverse for 'user-posts' with keyword arguments '{'username': ''}' not found. 1 pattern(s) tried: ['user/(?P<username>[^/]+)$']
Following is my urls.py codes
from django.urls import path
from .views import (PostListView, PostCreateView,
PostUpdateView, PostDetailView, PostDeleteView, UserPostListView)
from . import views
app_name = 'posts'
urlpatterns = [
path('', PostListView.as_view(), name="blog-home"),
path('user/<str:username>', UserPostListView.as_view(), name="user-posts"),
path('post/<int:pk>/', PostDetailView.as_view(), name="post-detail"),
path("post/<int:pk>/", views.post_comment, name="comments"),
path('post/new/', PostCreateView.as_view(), name="post-create"),
path('post/<int:pk>/update', PostUpdateView.as_view(), name="post-update"),
path('post/<int:pk>/delete', PostDeleteView.as_view(), name="post-delete"),
path('about/', views.about, name="blog-about"),
]
The problem seems to be coming from my _posts.html file, but I cant resolve it.
here is the _posts.html codes
<div class="media">
<h3 class="mr-5">#{{ post.user.username }}</h3>
<div class="media-body">
<strong>{{ post.user.username }}</strong>
<h5>{{ post.message_html|safe }}</h5>
<time class="time">{{ post.created_at }}</time>
{% if post.group %}
<span class="group-name">in {{ post.group.name }}</span>
{% endif %}
</h5>
<div class="media-footer">
{% if user.is_authenticated and post.user == user and not hide_delete %}
<a href="{% url 'posts:post-delete' pk=post.pk %}" title="delete" class="btn btn-simple">
<span class="fa fa-remove text-danger" aria-hidden="true"></span>
<span class="text-danger icon-label">Delete</span>
</a>
{% endif %}
</div>
</div>
</div>
Once I click on the group's list page to take me to posts associated with the group, I get the above error.
Someone said the username I am passing to the urls.py is empty, i cant figure out what is wrong with code from my views.py
from django.contrib import messages
from django.contrib.auth.mixins import (LoginRequiredMixin, UserPassesTestMixin)
from django.contrib.auth.models import User
from django.urls import reverse_lazy, reverse
from django.http import HttpResponseRedirect, Http404
from django.shortcuts import render, get_object_or_404, redirect
from django.views.generic import (ListView, DetailView, CreateView,
UpdateView, DeleteView)
from .models import Post, Comment
from django.core.files.storage import FileSystemStorage
from .forms import CommentForm, PostForm
from . import models
def home(request):
context = {
'posts': Post.objects.all()
}
return render(request, 'blog/home.html', context)
def upload(request):
if request.method == 'POST':
form = PostForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return redirect('post_detail')
else:
form = PostForm()
return render(request, 'blog/post_detail.html',
{'form': form })
class PostListView(ListView):
model = Post
template_name = 'blog/home.html'
context_object_name = 'posts'
ordering = ['-date_posted']
paginate_by = 6
class UserPostListView(ListView):
model = Post
template_name = 'blog/user_posts.html'
context_object_name = 'posts'
paginate_by = 6
def get_queryset(self):
user = get_object_or_404(User, username=self.kwargs.get('username'))
return Post.objects.filter(author=user).order_by('-date_posted')
class PostDetailView(DetailView):
model = Post
def post_comments(request, slug):
template_name = 'blog/post_detail.html'
post = get_object_or_404(Post, slug=slug)
comments = post.comments.filter(active=True)
new_comment = None
# Comment posted
if request.method == 'POST':
comment_form = CommentForm(data=request.POST)
if comment_form.is_valid():
# Create Comment object but don't save to database yet
new_comment = comment_form.save(commit=False)
# Assign the current post to the comment
new_comment.post = post
# Save the comment to the database
new_comment.save()
else:
comment_form = CommentForm()
return render(request, template_name, {'post': post,
'comments': comments,
'new_comment': new_comment,
'comment_form': comment_form})
class PostCreateView(LoginRequiredMixin, CreateView):
model = models.Post
fields = ['title', 'content', 'group', 'image']
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
class PostUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
model = Post
fields = ['title', 'content']
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
def test_func(self):
post = self.get_object()
if self.request.user == post.author:
return True
return False
class PostDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
model = Post
success_url = reverse_lazy('blog-home')
def test_func(self):
post = self.get_object()
if self.request.user == post.author:
return True
return False
def about(request):
return render(request, 'blog/about.html', {'title': 'About'})
There is nothing wrong with your urls.py and template. The problem is with '{'username': ''}' because username is empty it won't match with the regex(user/(?P<username>[^/]+)$). try to find out why username is empty.

Search in django doesn't work

I am trying to have search option i my page. I have tried the method given in the standard django document in this link 'https://docs.djangoproject.com/en/1.11/intro/tutorial01/', but my search function isn't working. only the url changes from http://localhost:8000/contact this to http://localhost:8000/contact/your-contact/?q=harini but datas are not filtered and displayed. I am using django 1.11. Could anyone help me with it? Thanks in advance
models.py
from __future__ import unicode_literals
from django.db import models
class ContactModel(models.Model):
name = models.CharField(max_length=200)
dob = models.DateField()
phone_number = models.IntegerField()
gender = models.BooleanField()
address = models.CharField(max_length=200)
views.py
from __future__ import unicode_literals
from django.shortcuts import render
from django.http import HttpResponseRedirect, HttpResponse
from django.views import generic
from django.template.response import TemplateResponse
from django.views.generic import CreateView
from django.urls import reverse_lazy
from .models import ContactModel
from .forms import ContactForm
class ContactView(CreateView):
model_class = ContactModel
form_class = ContactForm
success_url = reverse_lazy('contact-thanks')
initial = {'name': 'value'}
template_name = 'contact/contact.html'
def get(self, request, *args, **kwargs):
model = self.model_class()
return render(request, self.template_name, {'model': ContactForm})
def post(self, request, *args, **kwargs):
if request.method == 'POST':
form = ContactForm(request.POST)
if form.is_valid():
form.save()
return HttpResponseRedirect('thanks/')
else:
form = ContactForm()
return render(request, 'contact.html', {'form': form})
class ContactlistView(CreateView):
model = ContactModel
template_name = 'contact/contactlist.html'
def search(request):
query = request.GET['q']
t = loader.get_template('template/contact/contactlist.html')
c = Context({ 'query': query,})
if query:
results = Contact.objects.filter(Q(name__icontains=query) |
Q(dob__icontains=query) |
Q(address__icontains=query) |
Q(gender__icontains=query) |
Q(phone_number__icontains=query))
return render(request, 'contactlist.html', {'form': form})
else:
return render(request, 'contactlist.html', {'form': form})
urls.py
from django.conf.urls import url
from . import views
app_name = 'contact'
urlpatterns = [
url(r'^$', views.ContactView.as_view(), name='contact'),
url(r'^your-contact/$', views.ContactView.as_view(), name='contact'),
url(r'^your-contact/thanks/$', views.ContactView.as_view(), name='contact'),
url(r'^contact-list/$', views.ContactlistView.as_view(), name='contactlist'),
url(r'^contact-list/your-contact/$', views.ContactlistView.as_view(), name='contactlist'),
]
templates/contact/contactlist.html
<form method="get" action="contact-list/">
<input type="text" id="searchBox" class="input-medium search-query" name="q" placeholder="Search" value= "{{ request.GET.q }}">
{% if contact %}
{% for each_contact in contacts %}
<h3>Name: {{ contact.name }}</h3>
<p>Dob: {{ contact.dob }}</p>
<p>Gender: {{ contact.gender }}</p>
<p>Address: {{ contact.address }}</p>
<p>Phone Number: {{ contact.phone_number}}</p>
{% endfor %}
{% endif %}
<input type="submit" class="btn" value="Search" >
</form>
Your form action is pointed to your-contact/
<form method="get" action="your-contact/">
So this URL calls the view ContactView(), whereas the search query process is in the ContactlistView() view
class ContactlistView(CreateView):
''' '''
def search(request):
query = request.GET['q']
A solution is:
a little edit in your URLs patterns:
url(r'^contact-list/$', views.ContactlistView.as_view(), name='contactlist'),
And change the form action url and point it to this view ContactlistView(): with contact-list/
<form method="get" action="contact-list/">
Don't forget to replace if q: with if query:
I think the view as you have it right now is doing nothing, you should modify it in:
class ContactlistView(CreateView):
model = ContactModel
template_name = 'contact/contactlist.html'
def get(self, request):
query = request.GET['q']
t = loader.get_template('template/contact/contactlist.html')
c = Context({ 'query': query,})
if query:
results = Contact.objects.filter(Q(name__icontains=query) |
Q(dob__icontains=query) |
Q(address__icontains=query) |
Q(gender__icontains=query) |
Q(phone_number__icontains=query))
return render(request, 'contactlist.html', {'form': form})
else:
return render(request, 'contactlist.html', {'form': form})
Also please note a couple of things:
you're inheriting froma CreateView but you are not creating anything and you are not getting any advantage from that view, you should use a GenericView instead
you're defining the template as class attribute but then you are rendering you are doing everything manually because you are not getting (again) advantage of the CBV

how to set variable through views in django

how to set any variable in views so that its value i will get in template
home.html
<html>
<head>
<title>To-Do lists</title>
</head>
<body>
<h1>Your To-Do list</h1>
<form method="POST">
<input id="id_new_item" name="item_text" placeholder="Enter a To-Do item"/>
{% csrf_token %}
</form>
<table id="id_list_table">
<tr><td>1 : {{new_item_text}}</td></tr>
</table>
</body>
views.py
from django.shortcuts import render
from django.http import HttpResponse
from lists.models import Item
def home_page(request):
if request.method == 'POST':
new_item_text = request.POST['item_text'] #
print new_item_text;
Item.objects.create(text=new_item_text) #
else:
new_item_text = '' #
item = Item()
item.text = request.POST.get('item_text', 'A new list item')
item.save()
return render(request, 'home.html', {
'new_item_text': new_item_text
})
MODELS.PY
from django.db import models
class Item(models.Model):
text = models.TextField(default='')
This is the test that is failing
test.py
def test_home_page_returns_correct_html(self):
request = HttpRequest() #
response = home_page(request) #
self.assertIn('A new list item', response.content.decode())
expected_html = render_to_string('home.html',
{'new_item_text': 'A new list item'}
)
self.assertEqual(response.content.decode(), expected_html)
the error is
AssertionError: 'A new list item' not found in u'<html>\n\t<head>\n\t\t<title>To-Do lists</title>\n\t</head>\n\t<body>\n\t\t<h1>Your To-Do list</h1>\n\t\t<form method="POST">\n\t\t<input id="id_new_item" name="item_text" placeholder="Enter a To-Do item"/>\n\t\t\n\t\t</form>\n\t\t<table id="id_list_table">\n\t\t\t<tr><td>1 : </td></tr>\n\t\t</table>\n\t</body>\n</html>\n'
please tell me the solutions or any material(any link) for question that i asked
Templates get rendered with some context data, this is where you can pass through variables. In the case of Class Based Views as Imran suggested you can return your context from the
get_context_data method.
In function views you can pass context through to the render shortcut
return render(request, 'my-template.html', {"variable1": True, "variable3": foo})
you should use class based view
class HomeView(TemplateView):
template_name = 'home.html'
def get_context_data(self, **kwargs):
context = super(HomeView, self).get_context_data(**kwargs)
context['user']= self.request.user.username
return context
in view you can simply access user as
{{user}}

Categories

Resources