Hi i made some ModelForm. now when i enter i got this Error.
RuntimeError
Exception Value:
You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SLASH set. Django can't redirect to the slash URL while maintaining POST data. Change your form to point to 127.0.0.1:8000/comp/new/ (note the trailing slash), or set APPEND_SLASH=False in your Django settings.
What is causing this? May form action matches exactly the form URL comp/new and this match to my view to render and work with the data. But something went wrong....
My Url
url(r'^comp/new/$', 'company.views.add_company', name='add_company'),
My Form action
<form id="category_form" method="post" action="/comp/new/">
Probably your are using reverse so check your urls.py, the missing / should be there
Related
I am very new to Django and I'm nearing the end of the django girls tutorial. I have added "#login_required" above my post_detail in views (view for clicking on a specific post) and added a login.html template. So when I click on a post title I get redirected to my login page (so far, so good) and the url is: http://127.0.0.1:8000/accounts/login/?next=/post/11/ (trying this on my computer atm.)
Then I type in my admin name/password and automatically get redirected to http://127.0.0.1:8000/accounts/profile/ and of course get a "Page not found (404)" (since I have no url/view/template for that). I thought "Dang, I just wanted to be redirected to /post/11/"!
Looked around on stack overflow and found this question:
Signing in leads to "/accounts/profile/" in Django (sounds about right)
and got the answer
Change the value of LOGIN_REDIRECT_URL in your settings.py.
So I looked up LOGIN_REDIRECT_URL in the Django documentation:
Default: '/accounts/profile/'
The URL where requests are redirected after login when the contrib.auth.login view gets no next parameter.
This is used by the login_required() decorator, for example.
This setting also accepts named URL patterns which can be used to reduce configuration duplication since you don’t have to define the URL in two places (settings and URLconf).
Deprecated since version 1.8: The setting may also be a dotted Python path to a view function. Support for this will be removed in Django 1.10.
But doesn't my contrib.auth.login get a next parameter? (looking at my url that say "?next=/post/11/" at the end) Please help me out here, I'm lost for what the problem could be here :(
You can view the page at:
http://finbel.pythonanywhere.com/
And the source code at:
https://github.com/Finbel/my-first-blog
UPDATE (1):
So I now know that the LOGIN_REDIRECT_URL is the thing that's deciding where I end up next, which must mean that it ignores the next-parameter in the url. I googled further on the problem and found this question which was very similar to my problem, i.e.
Documentation states that I need to use the "next" parameter and context processors. I have the {{next}} in my template, but I'm confused on how to actually pass the "/gallery/(username)". Any help would be greatly appreciated.
(I don't even have the {{next}} in my template, where/how should I add it?)
The preferred answer to that question seemed to be:
Django's login view django.contrib.auth.views.login accepts a dictionary named extra_context. The values in the dictionary are directly passed to the template. So you can use that to set the next parameter. Once that is done, you can set a hidden field with name next and value {{ next }} so that it gets rendered in the template.
But I'm not sure how to interpret this. While writing this edit I got an answer on this post (by kacperd) and will read it through now)
The problem is that contrib.auth.login doesn't get the next parameter.
When you try to get the login_required view without credentials your request is redirect to login view, and the template you created is rendered. The next parameter is present in this view, but when you perform the login action which is submitting the form, you are not including next in your request so contrib.auth.login doesn't get it and redirects to default page.
The solution to your problem is to include the next param and pass it forward. You can do this by modifying your login template. Simply add ?next={{ request.GET.next }} to form action attribute.
<form method="post" action="{% url 'django.contrib.auth.views.login' %}?next={{ request.GET.next }}">
I am following the article in the djangobook on Forms:
http://www.djangobook.com/en/2.0/chapter07.html
the search url in the article doesn't redirect it to my app(book) url. Instead it goes to 127.0.0.1:8000/search, it should have been 127.0.0.1:8000/book/search
I think you should specify the url on the form tag
<form action="/book/search/?" ...>
And make sure it points to the right address / view /url.py and all that
i am trying to define a url pattern in django urls.py like
url(r'^networking$','mysite1.networking.views.networking'),
when i am typing http://myhost.com/networking in my address bar to go to networking page
i am getting 404 error and a slash '/' automatically added to the address bar like
http://myhost.com/networking/
help me out what i am doing wrong?
You probably aren't including your urlconf correctly. The behavior you're seeing is because of APPEND_SLASH is set to True by default when Django can't resolve the url.
Either set Append_Slash to false which is true by default or use your url description like given below which redirect url with slash to desired view.
url(r'^networking/$','mysite1.networking.views.networking'),
Seems your Apache server or some Django middleware is adding trailing slashes. You can either correct that, or the better way is you can use the following url pattern:
url(r'^networking/?$','mysite1.networking.views.networking'),
I'll give the step by step info.
Let's say we're in the about page, the URL is example.com/about.
There's an email a friend button, when clicked, the URL is example.com/emailafriend.
Then when I clicked the submit button, the referal URL will be submitted is example.com/emailafriend. Question, how to get the about page URL? BTW, I'm using request.META['HTTP_REFERER'].
You could work with the request path attribute and add that to your form's action attribute, like so:
<form method="post" action="/tell-a-friend?return_url={{ request.path }}">
...
</form>
Then, in your tell-a-friend view, HttpResponseRedirect to the return_url query parameter.
It looks like maybe you are using APPEND_SLASH which will do a redirect if you don't have a slash. Try changing the button link to sample.com/emailafriend/, note the ending slash.
Maybe simply pass sample.com/about as GET or POST param? Or even through session.
I generally add a catch-all 404 regex to my Django websites as the last expression in my urls.py:
urlpatterns += patterns('django.views.generic.simple',
(r'^.', 'direct_to_template', {'template': 'unknown.html'}),
I'm generally happy with the performance of this. The unknown.html template extends my base template and nicely tells the viewer that their entered url doesn't exist, but the page still has all the navigation and style of my website.
However, after having to repeatedly tell people to enter a trailing slash, I feel that the APPEND_SLASH = True parameter in settings.py needs to be set.
the docs state:
If APPEND_SLASH is True and the
initial URL doesn’t end with a slash,
and it is not found in the URLconf,
then a new URL is formed by appending
a slash at the end. If this new URL is
found in the URLconf, then Django
redirects the request to this new URL.
Otherwise, the initial URL is
processed as usual.
So following this logic, foo.com/bar is successfully caught by my "404" url expression before it can be redirect to my foo.com/bar/ url expression.
*What is the best way to maintain a friendly/custom catchall 404 page while also being able to use APPEND_SLASH or something with similar functionality?*
--edit/answer--
Somehow I missed that you just need to add a template named 404.html, and also make sure DEBUG = False
Thanks DTing!
I think you can just customize your 404.html instead of using a "catchall" since you are just redirecting to a custom template. There is no reason why your custom 404.html template can't extend your site's base.html.
http://docs.djangoproject.com/en/dev/topics/http/views/#customizing-error-views
Three things to note about 404 views:
The 404 view is also called if Django doesn't find a match after
checking every regular expression in
the URLconf.
If you don't define your own 404 view -- and simply use the default,
which is recommended -- you still have
one obligation: you must create a
404.html template in the root of your template directory. The default 404
view will use that template for all
404 errors. The default 404 view will
pass one variable to the template:
request_path, which is the URL that
resulted in the 404.
The 404 view is passed a RequestContext and will have access to
variables supplied by your
TEMPLATE_CONTEXT_PROCESSORS setting
(e.g., MEDIA_URL).
If DEBUG is set to True (in your settings module), then your 404 view
will never be used, and the traceback
will be displayed instead.
if you do want to use a custom view,
This page_not_found view should
suffice for 99% of Web applications,
but if you want to override the 404
view, you can specify handler404 in
your URLconf, like so:
handler404 = 'mysite.views.my_custom_404_view'