Friday, February 21, 2020

Re: understanding urls, forms, and HTTP ERROR 405

On Fri, Feb 21, 2020 at 2:24 AM Phil Kauffman <kauffman.phil@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 
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.

On Thursday, February 20, 2020 at 1:16:07 PM UTC-5, OnlineJudge95 wrote:


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...@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.

--
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+unsubscribe@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.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAD%3DM5eQvbc--tMz%2BgnjW%3DydudFxivOajJoz6MgDDRHKCmRxAuA%40mail.gmail.com.

No comments:

Post a Comment