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),
)
Related
I am trying to import views from my apps into the urls.py file. For awhile, I was able to using "from app_name.views import view_name", but for some reason now it is not recognizing the app name.
I did not change anything in my settings.py file so I'm not sure what caused this. I did delete some migrations and an my database.
One thing that is weird is that I can import them if I go up a folder so, "from src.appname.views" and this seems to work, but is not what I want since by base directory is the src folder.
from django.contrib import admin
from django.urls import path
from pages.views import home_page, history, more_information, prop_analysis
from Product.views import property_analysis_tools, property_analysis_results
urlpatterns = [
path('admin/', admin.site.urls),
path('', home_page),
path('home', home_page),
path('history', history),
path('information', more_information),
path('analyze', prop_analysis),
path('results', property_analysis_results)
Another odd thing is that when I run the server, all of these views seem to work. Can Django sometimes give off false errors?
In Django in order to import an app.view into project urls, we create an additional urls.py in every app and link that urls.py into
our project urls.py , just as shown below
change project urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('pages.urls', namespace="pages")),
]
create another urls.py in your pages app
as below, similarly in any other apps.
from django.urls import path
from .views import (home_page,history,....)
app_name = 'pages'
urlpatterns = [
path(' ', homepage , name='homepage-view'),
path('history/',history, name='history-view'),
]
Every other view from respective app.views will be included in corresponding app.urls [if u have multiple apps]
nB: changes may occur according to your settings.py configurations,
for more, read this django URL documentation
watch Corey Schafer playlist best Django tutorial for beginners
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 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
I have a question about urls.py in Django. I am building a blog from scratch as a way of learning Django myself. In the main urls.py file, I have specified the include path to my app's urls.py.
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^', include('myblog.urls')),
)
In the app (called myblog), the urls.py reads as follows:
from django.conf.urls.defaults import *
from models import blogmodel
from django.contrib import admin
urlpatterns = patterns('',
(r'^login/', include(admin.site.urls)),
(r'^$', include('myblog.views.getLatest')),
)
where getLAtest is the function in my views.py. The error says No module named getLatest
Here's my views.py,
from django.shortcuts import render_to_response
from myblog.models import blogdb
def getLatest(request):
post = blogdb.objects.all()
sorted_post = post.order_by('-served_date')
return render_to_response('blogs.html', {'posts':sorted_post})
Any help is appreciated. Thanks in advance..
You are using the wrong directive; include() is used to include another package; Django will look for a urls.py within the package myblog.views.getLatest when you use that directive.
You want to name the view itself instead:
urlpatterns = patterns('',
(r'^login/', include(admin.site.urls)),
(r'^$', 'myblog.views.getLatest'),
)
Note: no include() is being used.
Try updating this:
urlpatterns = patterns('',
(r'^login/', include(admin.site.urls)),
(r'^$', include('myblog.views.getLatest')),
)
to this:
urlpatterns = patterns('',
(r'^getLatest/$', 'myblog.views.getLatest'),
)
include is meant to read in another urls.py file, where you you are wanting to execute a specific view function.
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