understanding urls, forms, and HTTP ERROR 405

158 views
Skip to first unread message

Phil Kauffman

unread,
Feb 20, 2020, 1:06:48 PM2/20/20
to Django users
Hello,

Newbie in need of a little shove. It seems I need to review the purpose of the urls.py file. At present I am getting an HTTP Error 405 with the following:

urls.py:
path('', views.show_site, name = 'home'),
path
('site-view', views.List.as_view(), name='site-view')

views.py
class List(ListView):
   
def get_queryset(self, *args, **kwargs):
       
return Profile.objects.filter(sitename_id=self.kwargs['pk'])
def show_site(request):
    form
= SelectSite()
   
if request.method == 'POST':
        form
= SelectSite(request.POST)
       
if form.is_valid():
           
pass
   
else:
        form
= SelectSite()
   
return render(request, 'app/home.html', {'form': form})

home.html
{% extends 'app\base.html' %}

{% block title %}Site Home{% endblock %}
{% block content %}
<h1>This Form is Terrible</h1>
<form action={% url 'site-view' %} method="post">{% csrf_token %}
    <p> {{form}} </
p>
<input type="submit" value="Submit"/>
</form>
{% endblock %}

Any guidance would be greatly appreciated.

Thank You

onlinejudge95

unread,
Feb 20, 2020, 1:16:07 PM2/20/20
to django...@googlegroups.com
On Thu, Feb 20, 2020 at 11:38 PM Phil Kauffman <kauffm...@gmail.com> wrote:
Hello,

Newbie in need of a little shove. It seems I need to review the purpose of the urls.py file. At present I am getting an HTTP Error 405 with the following:
HTTP 405 error code states the the HTTP method is not allowed on the endpoint (https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405

urls.py:
path('', views.show_site, name = 'home'),
path
('site-view', views.List.as_view(), name='site-view')

views.py
class List(ListView):
   
def get_queryset(self, *args, **kwargs):
       
return Profile.objects.filter(sitename_id=self.kwargs['pk'])
You need to define the post() method yourself, 

def show_site(request):
    form
= SelectSite()
   
if request.method == 'POST':
        form
= SelectSite(request.POST)
       
if form.is_valid():
           
pass
   
else:
        form
= SelectSite()
   
return render(request, 'app/home.html', {'form': form})

home.html
{% extends 'app\base.html' %}

{% block title %}Site Home{% endblock %}
{% block content %}
<h1>This Form is Terrible</h1>
<form action={% url 'site-view' %} method="post">{% csrf_token %}
    <p> {{form}} </
p>
<input type="submit" value="Submit"/>
</form>
{% endblock %}

Any guidance would be greatly appreciated.

Thank You

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/10be0e68-da3f-42cd-a095-96d6d2a5617f%40googlegroups.com.

Phil Kauffman

unread,
Feb 20, 2020, 3:53:07 PM2/20/20
to Django users
So something like this?
def site(request, site_id):
    site
= get_object_or_404(Site.name, pk=site_id)
   
def sitesubmit(request):
   
#    site=get_object_or_404(Site, pk=site_id)

    form
= SelectSite()
   
if request.method == 'POST':
        form
= SelectSite(request.POST)
       
if form.is_valid():

            instance
= form.save(commit=False)
            instance
.name = site.site_id
            instance
.save()

   
else:
        form
= SelectSite()
   
return render(request, 'app/home.html', {'form': form})

I'm just learning this stuff. If you can think of any posts or examples please let me know.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

onlinejudge95

unread,
Feb 21, 2020, 3:34:30 AM2/21/20
to django...@googlegroups.com
On Fri, Feb 21, 2020 at 2:24 AM Phil Kauffman <kauffm...@gmail.com> wrote:
So something like this?
Yes if you are using method based views. You can still do the same using class-based views by referring to the following https://stackoverflow.com/questions/15622354/django-listview-with-post-method 
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ba1394b9-1fde-442e-9d4d-e67e32b2f694%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages