I want to be able to display a 404 page when any other request occurs. However, when I run the server and perform a POST request with the POSTMAN extension on chrome, I get a 403 display by Django and not the template I created.
proj/urls.py
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'', include("index.urls")),
url(r'auth/', include("userapp.urls"))
)
index/urls.py
urlpatterns = patterns('',
url(r'^$', load_index),
url(r'^admin/', include(admin.site.urls)),
)
index/views.py
def load_index(request):
if request.method == "GET":
form = RegistrationForm(auto_id=False)
return render(request, "index/index.html", {"form": form})
else:
# output 404 error
return render(request, "error/404.html", status=404)
When you create a Django web application, POST/PUT/DELETE requires the csrf token. It's kind of a security policy which Django follows.
Now, why is this required? Because, django web applications by conventions are app's that would have been run with keeping in mind the context of display and usage.
What you are trying to use is the Client like POSTMAN which is used mainly to test the REST services. (It does not mean you cannot fire requests for an web application that is hosted to work as an Html template).
Now, if you want to provide a POSTable API from your Django web application, consider the #csrf_exempt. The best name given for a topic: csrf-protection-should-be-disabled-for-just-a-few-views.
If you want your application to have, it's usage as a RESTFul service, make sure you use the right tool, might be Django Rest Framework. (Note: Using #csrf_extempt is also a way for you, if you are providing an API to your site, which would provide some views as an API for POST/PUT).
Related
Homepage doesn't load properly
I have successfully build and deployed my code in Heroku.
It works fine locally but react doesn't render the index.html
The backend API works properly here
but the homepage here doesn't load - using ReactJS.
My all GitHub codes are here inside the develop branch.
Followed this post steps to host the same.
The homepage does not have a proper URL or path defined.
For example, you can go here:
https://mangya.herokuapp.com/administrator/
Or here
https://mangya.herokuapp.com/api
...As they are valid URLs.
Your blank 'homepage' path is not listed so there is no view being hit thus you get the error.
To fix this you need to change your urls.py in MyBlog to look like this:
urlpatterns = [
path('', [path.to.your.view]),
path('administrator/', admin.site.urls),
path('api/', include(router.urls))
]
In Django for anything to be rendered when you hit a URL, you need to have a view defined that renders the template, this is what my example for your urls.py shows.
So for example, if you are trying to run a Vue, React, or any other frontend framework that would rely on an API and Ajax calls to populate a page, you must allow that base page to be rendered by Django as it is the server running your application.
I develop an application using Django and React. I want to serving Django and React together. I create an build in react. I use it in Django. This is urls.py ;
urlpatterns = [
url(r'^$', views.index),
]
and views.py;
def index(request):
return render(request, "build/index.html")
When I run development server there is no any error but when ı want to go url directly ;
http://www.x.com:8000/accounts
I got page not found (404) error.
Well, do you have an urlpattern for /accounts? If your example is complete then you get 404 because Django doesn't know what to return for /accounts. The only URL your Django server supports, as per your example code, is index available at http://www.x.com:8000 (maybe even http://www.x.com:8000/ (notice the trailing /) depending on your APPEND_SLASH settings).
You need to to add an urlapptern and view (and probably a template) for /accounts. Something like (not tested)
urlpatterns = [
url(r'^$', views.index),
url(r'^accounts$', views.index),
]
def accounts(request):
return render(request, "build/accounts.html")
I have a Django backend that returns json data. I'm able to get data back on my localhost but got a 404 on production server. I'm running nginx in front of gunicorn server. Any ideas why I'm getting a 404? Shouldn't this be able to work to retrieve json data, or do I need to use django rest framework and implement viewsets to make this work?
Not Found
The requested URL /about was not found on this server.
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^about', about.get_info),
]
about.py
from django.http import JsonResponse
def get_info(req):
return JsonResponse({"test": "hello"})
The problem is inside url.py. The way the rules are defined currently, it would only allow you to open about/ and admin/, i.e. with the / at the end. To fix this, you can define the URLs as following:
urlpatterns = [
url(r'^admin/$', admin.site.urls),
url(r'^about/$', about.get_info),
]
Now you should be able to use both admin/ and admin to access the page.
I am currently building a React with a Django REST backend. I have come into this one little problem I can't get past that has to do with Routing.
Here is my urls.py file.
urlpatterns = [
url(r'^api/', include(router.urls)),
url(r'^admin/', admin.site.urls),
url(r'^djangojs/', include('djangojs.urls')),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'^$', TemplateView.as_view(template_name='exampleapp/itworks.html')),
url(r'^(?:.*)/?$', TemplateView.as_view(template_name='exampleapp/itworks.html')),
]
Using this, it lets the react router do it's thing on the front end. For example, if I wanted to go to 127.0.0.1:8000/mentors then it will take me to the page I have set for React Router.
However, doing API calls in the frontend also returns the react page rather than the API endpoint because of this. So whenever I remove the last line in the code above: url(r'^(?:.*)/?$', TemplateView.as_view(template_name='exampleapp/itworks.html')),, then it gets the API returned in JSON format successfully. Problem is now when I try to go to the links it will return the Django 404 page rather than the React page I set in the React Router.
Is there anyway I can get the best of both worlds?
I have a Django CMS Project, which needs to create a Non CMS App "Achievemnets". The Customer wants full control over the page design, that means the page should be a CMS Page. However I have created specific views to show all the achievemtns in a page and clicking on the more link, it will show in detail. I need to port it to Django CMS I have tried as per the CMS App Hook method in the Django CMS Documentation. But none of them works.
Please tell me a tutorial which is good for learning CMS App Hooking
When you "hook" an application's URLs to a Django-CMS page, your app's URLs and view functions take over from there.
Let's say your Django-CMS page URL is: /achievements/
On this page, you want to display a list of achievements, which is going to come from your application.
#your_app.urls
from django.conf.urls.defaults import url, patterns
urlpatterns = patterns('your_app.views',
(r'^$', 'index'),
)
#your_app.views
from django.shortcuts import render
from your_app.models import Achievement
def index(request):
achievements = Achievement.objects.all()
return render(request, 'achievements/index.html',
{'achievements' : achievements})
The Django-CMS app hook you write tells Django-CMS which URLs to follow after in addition to the page you hook your app to. So, not only is Django-CMS going to pull content for the page by the slug, it's also going to hand off the matching URL pattern to your app.
I hope that makes sense.