Form on each page problem when upgrading from Django 1.4

40 views
Skip to first unread message

yakka...@gmail.com

unread,
Nov 12, 2017, 12:56:53 PM11/12/17
to Django users
I'm in the process of upgrading a website from Django 1.4 and I'm run into a problem of getting my code that shows a form on almost every page migrated.  I eventually want to upgrade to Django 1.11 but I'm currently working on upgrading from Django 1.4 to Django 1.5.  I have a “request information” form on each page except for a few sign-up pages. 

In Django 1.4 I used direct_to_template for most of the website.  I have a <base.html> that is inheirted by each page.  For example <home.html>, <about-us.html>, etc. inherit  from <base.html>.  In <base.html> I load the right columns html with
    “{% block rightColumn  %} {%include "site/rightColumn.html"%}  {%endblock%}”
and then in <rightColumn.html> I load the form's html with
    “{%include "site/reqInfoForm.html"%}”.     

To get the from on each page I used middleware similar to this http://stackoverflow.com/questions/2734055/putting-a-django-login-form-on-every-page.  I created a class with a method “def process_view(self, request, view_func, view_args, view_kwargs)”.  In the method if a POST request and not one of the signup pages then I create a form instance from the request and if the form is valid then process it then redirect to thanks page, otherwise attach the form to the request so that I can show the errors in the form template. 

This worked perfectly in Django 1.4 though it seemed like a hack.

While upgrading to Django 1.5 in the url.py I switched from “direct_to_template” to “TemplateView.as_view”.  Now when there is an error in the form (i.e., leave a required field blank), I get an error “"POST / HTTP/1.1" 405 0” while running with the local server.  When I did some research, it appears TemplateView doesn't have a POST method.

It seems one way may be to add TemplateView for each page but it seems like I would have to add a post method for each page(or maybe a mixin).  Any thoughts on this method?  I found another method using context processors but I'm not following it.  Also any suggestions on other ways of adding a form to almost all pages?

Matthew Pava

unread,
Nov 13, 2017, 9:30:18 AM11/13/17
to django...@googlegroups.com

That is an interesting setup.  We just use login middleware that automatically redirects to the LogIn view with a next GET parameter.

 

In your situation, I would probably treat the form as ajax and create an ajax view for handling the login form processing.  Just change your action URL to your ajax login view url.  You would still include the form in the way you have and just add some Javascript to the form file.

--
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 post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/52c882b9-09d5-4b58-8234-b967785650de%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

yakka...@gmail.com

unread,
Nov 15, 2017, 8:45:48 PM11/15/17
to Django users

@Matthew,


It's not a login form. It's a normal form. I don't like the AJAX way. It adds moving parts and requires a front end rework (and more tests, browser compatibility tests, potential issues with plugins blocking, etc.).

yakka...@gmail.com

unread,
Nov 15, 2017, 8:46:15 PM11/15/17
to Django users
Anyone else have ideas using the Django backend?


On Sunday, November 12, 2017 at 12:56:53 PM UTC-5, yakka...@gmail.com wrote:
Reply all
Reply to author
Forward
0 new messages