Django project - Bad Request (400) on certain urls - python

This is first question here and I will try to follow the guidelines to the best of my understanding. ( Also, English is not my first language )
I made a little project using Django, a job search website and i'm running it with DEBUG set to True ( not advised I know, but I wanted to know the cause if anything went wrong )
The admin section and homepage ( and another one ) are working correctly, but the search page gives me a 400 error page. Bad Request (400).
The only difference with the other working page is that the search contains a form. The "heroku logs" doesnt give more detail about the error. Any advice about where I should look for the cause of the problem ?
Everything works correctly locally, I only get the 400 error once it's pushed on heroku. Help ?
As requested, Here are the urls of the search page:
url(r'^search/$',search,name="job-search"), #I use this to collect the search parameter
url(r'^search/(?P<domain>\w+)/(?P<level>\w+)/$',results,name="job-result"), # **Where the 400 error happens** , this is the result page

Make sure your method has some decorator that prevents access to it, like: #ajax_required

Related

How can I personalize an URL path in Django?

I am trying to build an website which renders some books and the corresponding pages. I want to make possible to access a page like this:
path('/<str:book_pk>-<int:book_page>/', views.TestClass.as_view(), name='book-test')
I want a user to access it very simple, something like: mysite.com/5-12/ - which redirects him to the book nr 5 at page 12. The problem is that when I access this page from the website itself, using href, the real path becomes:
mysite.com/%2F5-13/
If I want to write in the browser, the following path: myste.com/5-13/, it throws me 404 page not found, because the real path is mysite.com/%2F5-13/ . This is pretty obvious, but my question is:
How can I stick to my initial path, and make it possible to be accessed via myste.com/5-13/? For some reason, Django URL Patterns, adds an extra %2F string at the beginning. Can somebody explain me why, and how to solve this issue?
I much appreciate your time and effort! Thank you so much!
You don't have to include / at the beginning of the url, simply:
path('<str:book_pk>-<int:book_page>/', views.TestClass.as_view(), name='book-test')
/ is encoded automatically as %2F in urls (read the full list here)

Setting Django test cookie on every page

I am not a Django nor a Python programmer (I do know Perl, PHP, and Javascript pretty well though). I was given a Django site to work on and I understand the templating really well, I just never touch any actual Python code. My client wants a log-in on every page of the site, which is easy to do… but often when a user logs in, they get the error message "Your Web browser doesn't appear to have cookies enabled. Cookies are required for logging in." I guess that's because a test cookie is set when you go to the Django log-in page, and then when you log in, it checks for it. If you're not logging in from the log-in page/don't have the cookie set, you get the error. I tried a hack to load the login page in the background via AJAX to set the cookie and that for some reason only works about 50% of the time. Are there any Django/Python experts out there who can tell me step-by-step what a non-hack way would be to prevent this error from coming up?
Check this question for how to use Django to set persistent cookies.
Also check this question for how to get cookies.
I think the solution you need is the combination of both; create a test cookie under any name, set it, and then check to see if it exists prior to generating your view.

Unable to get Python to POST on html

I've been trying to get my python code to post. I have tried using the Postman Plugin to test the post method and I would get a 405 method error. I am planning to have the user post the information and have it displayed.
Currently if I press submit I would get a error loading page, changing the form to get results in the submit button working and returning to the previous page. If I change the handler to post the screen would instantly display '405 Method Not Allowed'. I've looked through the Google App Engine logs and there are no errors. Can someone help me with what I done wrong and advise me on how to the post method functioning?
Thanks for the time.
You're getting '405 method not allowed' because the POST is going to the same url that served up the page, but the handler for that path (MainPage) does not have a post method.
That's the same diagnosis that you were given when you asked this question two hours earlier under a different user id.
Stepping back further to address the "what have I done wrong" question: It seems to me that you've gotten quite far along before discovering that what you have doesn't work. So far along that the example is cluttered with irrelevant code that's unrelated to the failure. That makes it harder for you for figure this out for yourself, and it makes it more difficult for people here to help you.
In this situation, you help yourself (and help others help you) by seeing how much code you can eliminate while still demonstrating the failure.

Determine if website developed with Django [duplicate]

This question already has answers here:
How can you tell if a site has been made with Django?
(7 answers)
Closed last year.
I actually have some questions (real childish).
1) If I know that a website has been developed using django , can we determine from the html source code (by right clicking and choosing "view page source ") , if that website has been developed using django?
2) If I have an HTML code for website written in HTML , and I just want to present it like that using django , how can present this HTML code using django?
3) For what kind of websites , should django be used or are used for ? I mean pure static page , blogs , or a simple google like .
Thanks in advance
Here are a few things you could use to determine if a web app was written in Django. None of these are foolproof by any means, but they could be indicators.
Try http://site.com/admin/ and see if it says "Django site admin" at the top.
Inspect all of the HTML source code of every form you can find on the site, and see if any contain an input tag with name='csrfmiddlewaretoken'. csrfmiddlewaretoken is Django's CSRF token identifier. Other web frameworks may use this same name, but Django is the predominant user of it.
Find information about the site owner and/or developer from a "Contact" page, Google their usernames/emails as well as the word "Django," such as "emailname#gmail.com django". If you see posts or questions about Django, this could possibly mean they use it often.
If all else fails, simply contact the site owner and ask them.
No, the source depends completely on the person who developed it, and there are no necessary "hints" that it was written in Django.
You should at least try the Django tutorial at https://www.djangoproject.com/, you'll learn the basics of setting up a Django application, and you'll answer your own question.
Django is pretty general purpose, a bit overkill for static pages. Anything else can be done in Django, the same way it can be done in Ruby on Rails or other Web frameworks.
Well, what you should do is to test the website behaviour in an unusal stiatuation, for example forcing it to return 404 or 500 error message, which developers often forget to customize.
If you for example go to http://www.galaxyzoo.org/ and then try to determine backend just by looking at HTML, you'll fail.
If, however, you try to access a page 'blablablabla' i.e. http://www.galaxyzoo.org/blablablabla then you'll see 404 message. If you paste the entrie text into google, you'll most likely get hits to Ruby On Rails... :)
Django leaves no trace on the html source unless you specifically do. If you only want a static site, django is overkill. Though if you really want to, have a look at django flatpages.
You could possibly try www.domainname.com/admin. Some people leave their admin at that url and you can see the login page.
If they left the login page as default, the title tag will say Login | Django site admin or something like that.
For example: http://www.snowbird.com/admin/ (no affiliation)
No.
Yes. See direct_to_template
See djangosites.org

Facebook calling Google App Engine code using GET instead of POST

I've been developing a Facebook app using Google App Engine in Python and the pyfacebook bindings. For weeks everything worked fine but suddenly it stopped.
At first I thought it was a code change so I rolled back the entire dev directory to a version I knew worked, but still it failed. It's possible a change I made to the application's settings caused the issue but, if so, I can't figure out what.
I've figured out that the problem is that instead of calling the post(self) method of my Main class, Facebook is calling using a GET.
Does anyone know why Facebook would use a GET method instead of a POST? It's an IFrame app.
Thanks,
The typical flow for a user when using the application begins with the user landing at some Canvas URL, like http://apps.facebook.com/runwithfriends/. At this point, Facebook will load up it's chrome, and render a tag to your application. You'll notice there isn't a src specified. Using some JavaScript and the tag, Facebook triggers a POST request to your application. This is done for security reasons, as the sensitive user data won't be sent via the HTTP Referrer header as long it's sent as POST data.
Although I'm not completely sure this was the cause, it appears I changed from an FBML app to an IFrame app. FBML mode relies on POST calls but IFrame appears to use GET. I'm inferring this answer from what I read here as well as from the observations I'm seeing and this being the only answer that makes any sense.

Categories