I have a Django app (I'm fairly new so I'm doing my best to learn the ins and outs), where I would like to have a url endpoint simply redirect to a static html file in another folder (app).
My project file hierarchy looks like:
docs/
- html/
- index.html
myapp/
- urls.py
My urls.py looks like:
from django.conf.urls import patterns, include, url
from django.views.generic import RedirectView
urlpatterns = patterns('',
url(r'^docs/$', RedirectView.as_view(url='/docs/html/index.html')),
)
However, when I navigate to http://localhost:8000/docs I see the browser redirect to http://localhost:8000/docs/html/index.html, but the page is not accessible.
Is there any reason that the /docs/html/index.html would not be avalable to the myApp application in a redirect like this?
An pointers would be greatly appreciated.
NOTE: direct_to_template has been deprecated since Django 1.5. Use
TemplateView.as_view instead.
I think what you want is a Template View, not a RedirectView. You could do it with something like:
urls.py
from django.conf.urls import patterns, include, url
from django.views.generic.simple import direct_to_template
urlpatterns = patterns('',
(r'^docs/$', direct_to_template, {
'template': 'index.html'
}),
)
Just ensure the path to index.html is in the TEMPLATE_DIRS setting, or just place it in the templates folder of your app (This answer might help).
I'm pretty sure Django is looking for a URL route that matches /docs/html/index.html, it does not know to serve a static file, when it can't find the route it is showing an error
Related
I'm developing my first app in Django and facing some issues. The server runs smoothly and I can operate the admin panel without any issues.
However, all of the app pages including the default homepage show a "404 not found" error.
I have created a templates directory in project root
updated this line in settings.py for templates,
'DIRS': [os.path.join(BASE_DIR,'templates')],
View for the app
from django.shortcuts import render
from .models import *
# Create your views here.
def customers(request):
customers = Customer.objects.all()
return render(request, "customers.html", {'customers': customers})
urls for the app
from django.urls import path from . import views
urlpatterns = [path('customers',views.customers, name='customers')]
urls for the project
from django.contrib import admin from django.urls import path, include from django.conf import settings from django.conf.urls.static import static
urlpatterns = [
path('trips/',include('trips.urls')),
path('customers/',include('customers.urls')),
path('drivers/',include('drivers.urls')),
path('vehicles/',include('vehicles.urls')),
path('admin/', admin.site.urls), ]
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Help is appreciated
I can't see any problem with your case, you should be able to load http://localhost:8000/customers/customers. Maybe double customers was not you intention, then remove one of them (one in main urls.py one in app's).
Also when Django debug mode is active and you get a 404 because of URL mismatch, it shows you a list of URLs you have. To see it navigate to a non existing URL like http://localhost:8000/aaa. Then if you see customers/ there try http://localhost:8000/customers/. Go step by step to find the problem.
I am a beginner in Django I have created a Django project in that I have included two more application when I did add urls.py file in both Applications both are working well but when I am fetching my main admin URL it is giving an error
Page not found (404)
Request Method: GET
URL: http://127.0.0.1:8000/
the URLconf defined in mac.urls, Django tried these URL patterns, in this order:
admin/
shop/
blog/
The empty path didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to
False, and Django will display a standard 404 page.
when I am fetching this URL in http://127.0.0.1:8000/ i am getting an error it is working for http://127.0.0.1:8000/shop/
here is my main urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('shop/', include('shop.urls')),
path('blog/', include('blog.urls')),
]
Your django app has 3 routes:
http://127.0.0.1:8000/admin/ goes to django admin app
http://127.0.0.1:8000/shop/ goes to your shop app
http://127.0.0.1:8000/blog/ goes to your blog app
And since you have no configuration for http://127.0.0.1:8000, you see an error instead.
You can see that in the error, when django tries to match your url with list of available urls.
If you want to get admin app on url http://127.0.0.1:8000, change urls.py to:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('', admin.site.urls),
path('shop/', include('shop.urls')),
path('blog/', include('blog.urls')),
]
It's generally not advisable to set admin app at root url - it has it's own system of urls inside (admin/<app_name>/<model_name>), so chances are it will shadow your urls and make the unreachable.
Create a view that will be your front page.
From there you should link to the other areas of your website.
Don't direct that to admin, that's ridiculous.
You've created a Django project and started two apps. You should have a project-level urls.py file and then an app-level urls.py file for each of your apps.
To explain that in greater detail lets say our Django project is called config and our two apps are called app1 and app2. Your project-level urls.py file, which will be located at config/urls.py, could contain the following:
# config/urls.py
from django.contrib import admin
from django.urls import path, include
from django.views.generic.base import TemplateView
urlpatterns = [
path('admin/', admin.site.urls),
path('', TemplateView.as_view(template_name='home.html'),
name='home'),
path('app1/', include('app1.urls')),
path('app2/', include('app2.urls')),
]
In this file we specify a route for our admin panel, which on your local server will be located at http://127.0.0.1:8000/admin. We've also specified a home route, the second path with the empty string. This means when you navigate to http://127.0.0.1:8000/ you will be directed to your home page (for the example above I just used a generic built-in view). It's not a good idea to route immediately to your admin panel.
We've also included paths to our other two apps. What these two lines essentially say is: "include the urls from this other app". Now we need to create two urls.py files, one for each of our apps. In this example I'll just focus on the urls.py file for app1:
# app1/urls.py
from django.urls import path
from .views import AppContentView
urlpatterns = [
path('content/', AppContentView.as_view(),
name='app_content'),
]
This is a view that you would have to create, but what we've now done is we've created one path that will be located at http://127.0.0.1:8000/app1/content. In fact any new paths we create in this file will always begin with http://127.0.0.1:8000/app1/, because we've already told Django in our project-level urls.py to include the urls from the app1 urls.py file so we've essentially prefixed all of these paths with /app1/.
If you think of urls configurations like a tree it might help too:
Project Level Url Configs.
|
|
|
___________________________
| |
| |
App 1 Url Configs. App 2 Url Configs.
Thing is, I have an angular + cordova app on one module. I just want the REST api from django for the webserver. no views or anything (well only to serve the angular views).
How can I serve the static index.html using django? do I need different project structure? I'm a newbie to django.
Thanks in advance!
You can use TemplateView directly:
from django.views.generic import TemplateView
...
url(r'^$', TemplateView.as_view(template_name='index.html'))
Remember you need to configure your templates folder to call .html files by name only.
First, place this index.html page in your server/templates folder so Django knows about it.
Then, do this in your URLs.py:
from django.views.generic import TemplateView
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name='index.html')),
]
I have a working api, and I am writing a UI to the API, as a separate application in the same project. My project urls.py looks like this
from django.conf.urls import *
import search
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^api/search$', search.validation),
url(r'^ui', include('ui.urls')),
)
My UI app's urls.py looks like this
from django.conf.urls import *
import views
urlpatterns = patterns('',
(r'^ui/$', views.search_template),
)
However, when I am trying to access with my browser(domain.com:8000/ui), I am getting an error.
Using the URLconf defined in api.urls, Django tried these URL patterns, in this order:
^api/search$
^ui ^ui$
The current URL, ui, didn't match any of these.
But if I use the below mapping in the main project's urls.py, it works.
(r'^ui$', ui.views.user_template),
I tried clearing the urls.pyc to make sure it is not stale, but it still persists. Please let me know what am I doing wrong.
You shouldn't repeat ui regex in the app's urls.py:
urlpatterns = patterns('',
(r'^$', views.search_template),
)
I'm following the tutorial on the Django website but I'm trying to expand upon it. I like the organizational scheme of putting all of your apps in an "apps" folder. I'm trying to figure out the proper way to include urls.py in order to get everything to link together.
Here's my root urls.py:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^polls/', include('apps.polls.urls')),
(r'^admin/', include(admin.site.urls)),
)
Here's my urls.py at apps/polls/urls.py:
from django.conf.urls.defaults import *
urlpatterns=patterns('polls.views',
(r'^polls/$', 'index'),
(r'^polls/(?P<poll_id>\d+)/$', 'detail'),
(r'^polls/(?P<poll_id>\d+)/results/$', 'results'),
(r'^polls/(?P<poll_id>\d+)/vote/$', 'vote'),
)
What's the correct way to do this? Thanks!
The way you currently have it set up... the URLs for polls would be:
http://your.url.here/polls/polls/235/results/
This is probably not what you want. The include function in the urlpatterns in the root urls.py file specifies "polls/" as a prefix to all urlpatterns in the polls app. Therefore, in the polls/urls.py file, you shouldn't specify the "polls/" prefix again as it will cause duplicate prefixes.
How are you running your Django instances? If you have multiple vhosts configured in Apache then each Django instance in /apps has it's own urls.py.
I got it to work by doing this:
urlpatterns=patterns('polls.views',
(r'^$', 'index'),
(r'^(?P<poll_id>\d+)/$', 'detail'),
(r'^(?P<poll_id>\d+)/results/$', 'results'),
(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)
I guess the polls part is taken care of in the root urlconf