React and Django website not loading on Heroku - python

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.

Related

Django trying to match wrong url path

Trying to deploy my Django app on virtual shared server. All works as expected until I try to log in to the admin app. I think it may have something to do with the way that the server deals with posts (had a similar problem with Flask). The setup works fine on my local machine.
I can get to the admin login page, but when I submit my credentials I get a 404 error. In debug mode the response is:
Page not found (404)
Request Method: POST
Request URL: https://lindcraft.williesworkshop.com/admin/login/
Raised by: django.contrib.admin.sites.login
Using the URLconf defined in lindcraft.urls, Django tried these URL patterns, in this order:
admin/
^[P|p]roducts?/(?P<prod_id>\d{0,3})/$ [name='product']
^[P|p]rices?/?$ [name='prices']
^[P|p]arking [name='parkingIntro']
^[D|d]isplay [name='displayIntro']
^[C|c]ontact [name='contact']
^[A|a]bout [name='about']
^$ [name='index']
The current path, login/, didn't match any of these.
The project urls.py is:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('www.urls')),]
Django seems to be looking for "login/" instead of "admin/" even though the "request URL" is "... /admin/login/" The admin part is striped off somehow.
Funny thing is, the exception seems to be raised by django.contrib.admin.sites.login as though the request actually made it to admin.
It seems to me that the same URL works with a GET but not with a POST.
Can anyone offer any insight?
FWIW, my hosting service (a2hosing) uses something called passenger to manage python requests. There may, or may not be a ngnix server used too.
TIA!

How to ignore or redirect a http request in Django?

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.

URL Regular Expression mismatch

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.

Django 1.8 not using my custom login page

I am working Django 1.8 and having trouble using my custom login page. Whenever I go to my login url, the Django admin username and password form comes up instead of my custom login.html.
I have worked in the Django 1.7 and this works fine but I do not know why I am having the error in 1.8.
login.html is my custom login template.
My urls:
url(r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'admin/login.html'}, name='auth_login'),
I have checked the forums and the Google but still cannot find a similar error.
EDIT:
Answer thanks to Kishor.
In Django 1.8 I simply renamed the admin pages that I was using, namely, base.html, base_site.html and login.html [I store these files inside a folder called 'admin' which is inside my app's 'templates' folder]
Instead of using the normal way like in previous versions of Django i just called the three pages above myapp_base.html, myapp_base_site.html and myapp_login.html [Do not forget to also change the tag at the top inside these pages where is says 'extends']
From there my app was able to pick up that I wanted to override the standard Django login page and customize it to my specification.
Use this 1.8 way/generic view way
url(r'^accounts/login/$', auth_views.login, {'template_name': 'myapp/login.html'}),
and be sure that you have included
url('^', include('django.contrib.auth.urls'))

403 template displaying instead of 404

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).

Categories

Resources