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?
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 have a large API REST project with Django Rest Framework, and I want to document it with Django REST Swagger, but I want this Swagger documentation to include only some of the endpoints of my whole project.
This is my urls.py where I set Swagger in my project:
from rest_framework_swagger.views import get_swagger_view
urlpatterns = i18n_patterns(
path('api-token-auth/', views.obtain_auth_token),
path('', include('FrontEndApp.urls')),
path('admin/', admin.site.urls),
path('api-auth/', include('rest_framework.urls')),
path('docs/',get_swagger_view(title="Intellibook API")),
path('rosetta/', include('rosetta.urls')),
path('general/', include('GeneralApp.urls')),
path('operations_manager/', include('OperationsManagerApp.urls')),
path('payments_manager/', include('PaymentsManagerApp.urls')),
#path('providers_manager/', include('ProvidersManagerApp.urls')),
path('rates_manager/', include('RatesManagerApp.urls')),
path('reports_manager/', include('ReportsManagerApp.urls')),
path('reservations_manager/', include('ReservationsManagerApp.urls')),
path('users_manager/', include('UsersManagerApp.urls')),
path('excursions_manager/', include('ExcursionsManagerApp.urls')),
path('invoices_manager/', include('InvoicesManagerApp.urls'))
)
Currently, Swagger publishes all the endpoints that are in all urls.py along the whole project. I want to set it to publish only the endpoints in ursl.py of only on of the apps of the project.
Look into
SWAGGER_SETTINGS = {
"exclude_namespaces": [], # List URL namespaces to ignore
}
Unfortunately, I couldn't find the docs. But if I get this right, you should split your urlpatterns into two parts, one for published, second for non published.
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 use an Angular 4 frontend and Python Django in the backend.
If I click on the detail button I run the openDetail method in my component
openDetail(id:number){
this.router.navigate(['/motor/detail', {"id": id}])
}
the browser opens the detail component with the URL http://localhost:8000/motor/detail;id=21. Perfect. Important to know is, that I just need the id to work with them in my detail component.
But if I refresh the page I run into a 404 error. --> The current path, motor/detail;id=21, didn't match any of these. Well, this is also clear and jumped into the backend.
main - urls.py
#... some other urls...
url(r'^motor/', include('motorControll.urls')),
motors - urls.py
url(r'^$', views.index, name="index"),
url(r'^overview/$', views.MotorMapping.as_view({'get': 'getAllData'})),
url(r'^detail/$', views.index, name="index"),
url(r'^detail/(?P<id>\d+)/$', views.MotorMapping.as_view({'get': 'getById'})),
url(r'^detail/save/$', views.MotorMapping.as_view({'post': 'save'})),
How can I ignore this call or run a redirect to the index page?
I need the id, because the next step, after load the detail page is to load the details by this url http://localhost:8000/motor/detail/21 , it returns JSON with all data.
Your URL mapping in motors.urls is configured properly for the URL /motor/detail/21 -- so it works. But it's not configured for a URL like /motor/detail;id=21.
If you added another route to motors.url so that /motor/detail;id=21 works -- something like url(r'^detail;id=(?P<id>\d+)$', views.MotorMapping.as_view({'get': 'getById'})), then the URL would return raw JSON, so that's NOT what you want.
Instead, the actual solution is more complicated -- you want the URL to be routed to your single page app, and not Django, and have Angular take care of loading the data from Django Rest Framework.
I am trying to learn Django and I am currently stuck in an issue.
I created an app Contact and run the server, I get the error.
The error page displayed by server:
The urls.py file in the app Contact
urls.py in conatct
When the pattern in urls.py is
urlpatterns =[url(r'^$', views.form, name ='form')]
it works properly, but not with other pattern shown in the picture
Your help would be greatly appreciated.
The Page not found error message tells you what went wrong: For the URL (/contact) you requested, Django was unable to find a suitable view. Because you have debugging enabled, you get some information, including a list of registered views.
First things first: You probably have url(r'^contact/', include('contact.urls')) somewhere in your top level urls.py. This makes the URLs defined in the contact/urls.py available under the prefix /contact.
With
urlpatterns = [
url(r'^form/', views.form, name='form'),
]
in contact/urls.py you are telling Django that you want urls starting with contact/form/ to be handled by views.form.
Consequently, when you access http://localhost:8000/contact/ in your browser, there is no view associated with that URL, hence the 404. Your view is reacting to to http://localhost:8000/contact/form, not http://localhost:8000/contact.
When you change the URL pattern to
urlpatterns = [
url(r'^$', views.form, name='form'),
]
you modify the URL views.form reacts to.