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")
Related
I'm trying to deploy gatsby based frontend with django based backend under a single domain. It will rely on Apache and mod_wsgi. In a perfect world it should work as following:
https://my-domain.com/ - gatsby frontend
https://my-domain.com/admin - django
https://my-domain.com/api - django
I can see two possibilities:
Django is aware of frontend. Serve everything via Django, setup / as a STATIC_URL.
Django is not aware of frontend. Serve /api and /admin via django. / is handled by a webserver.
I feel more comfortable with the second approach, however I do not know how to configure VirtualHost for such a scenario. The firstch approach looks like an ugly hack.
How should I proceed with that?
After compiling your gatsby project, it should be served by django as a static page.
First: The gatsby dist should be in your static_private path.
Second: In your django project, you will define a URL for / that will call an index view let's say.
Finally: in your view you should render index.html of your gatsby dist.
urls.py:
from django.contrib import admin
from django.urls import path, re_path, include
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('apis/', include('apps.urls')),
path('/', views.index),
]
views.py:
from django.shortcuts import render
def index(request):
return render(request, 'index.html')
Note that if you are handling routing in your frontend your url pattern for the index view should be like this : re_path('^.*$', views.index)
If you are hosting your django app on heroku you will need the whitenoise middleware and set it up in your settings.py :
MIDDLEWARE = [
...
'whitenoise.middleware.WhiteNoiseMiddleware',
...
]
A doc is available here: https://devcenter.heroku.com/articles/django-assets#whitenoise
I want to connect my angular 2 app to django but how can i connect because both have different servers. I also read about cors but didn't work. Please suggest me some simple way to connect both of them
views.py
# Home Page
def index(request):
return render(request, "index.html")
urls.py
from django.conf.urls import url
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^admin/', admin.site.urls),
]
.angular-cli.json
"root": "src",
"outDir": "../userRecord/templates",
"assets": [
"assets",
"favicon.ico"
],
"index": "index.html",
userRecord is Django Server
Thanks in Advance
Hello I found a GitHub Repo without any Error
Here I posting the URL of that repo
https://github.com/badcoder28/djangular2
There's no problem serving the django backend and the angular frontend from different domains, but you do have to set up CORS correctly for it to work.
To configure that, you'll have to setup Django to return the CORS headers and allow the domain where your frontend lives. These might be helpful:
https://github.com/ottoyiu/django-cors-headers
http://www.django-rest-framework.org/topics/ajax-csrf-cors/
You need to serve angular app using django server.
In order to do this you need to keep angular bundled file in django static directory.
If you are using angular cli then you need to change outDir: "../../static/app" property in .angular-cli.json file to your django static directory, so whenever you build your app, your compiled file will be in django static directory.
Refer these static files into your django html page which django serve whenever you launch your app.
Happy Coding
EDIT
In urls.py you can configure like this
urlpatterns = [
url(r'^', views.index, name='index')
]
In your views.py have the index action like this.
def index(request):
return render(request, "your template path")
Make sure your def index action should return index.html.
I hope it will help you.
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 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.
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).