I created a project and created an application orders.
Views.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
return render(request, 'orders/index.htm')
def orders(request):
return render(request, 'orders/order.html')
urls.py:
from django.conf.urls import patterns, url
from orders import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^index.htm/', views.index, name='dashboard'),
url(r'^order.html/', views.orders, name='orders'),)
I used a ready to use site template.the link for 2 pages are:
index.htm index.htm is Dashboard
order.html order.html is Orders
when i go to link localhost:8000/orders/ , I click on orders the link looks like this.
and further click on two links in left pane multiple times, the resulting url look like this:
localhost:8000/orders/order.html/order.html
localhost:8000/orders/order.html/index.htm
I want to remove the order.html from middle so that it looks like:
localhost:8000/orders/index.htm
localhost:8000/orders/order.html
The links in your template do not have a leading slash, which means they are relative to the current directory.
If you are on the page with the URL http://localhost:8000/index.html/ and click on a hyperlink with href="order.html", it will take you to
http://localhost:8000/index.html/order.html
To fix it you must either
Add a slash to the links in your template, e.g. <a href="/order.html">
or drop the trailing slash from your URL definition in urls.py, e.g.
url(r'^index.htm', views.index, name='dashboard')
I suggest you use the template tag url in your templates instead of hard coding links.
If possible, I would also remove the .htm extension from both templates and urls.py
Change the following in urls.py file
from django.conf.urls import patterns, url
from orders import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^index/$', views.index, name='dashboard'),
url(r'^order/$', views.orders, name='orders'),)
Related
I have recently started using Django and I am a little lost as to why my services.html page does not render as I have followed what appears to be the same steps I took for my home page to work but I keep getting the error that nothing matches the current path. Below are my snippets for views and urls.py. I also had it be included in my website urls.py by using path('', include('affiliate.urls')),
Using the URLconf defined in website.urls, Django tried these URL patterns, in this order:
admin/
[name='home']
[name='services']
The current path, services.html, didn't match any of these.
Urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name="home"),
path('', views.services, name="services"),
]
Views.py
from django.shortcuts import render
# Create your views here.
def home(request):
return render(request, 'home.html', {})
def services(request):
return render(request, 'services.html', {})
Change your urlpatterns as follows:
urlpatterns = [
path('', views.home, name="home"),
path('services/', views.services, name="services"),
]
Now when you want to access the services view, make sure to use 127.0.0.1:8000/services
because both of your paths for home and services are pointing to the same url.
Willem is trying to explain you the same thing.
I checked other pages to figure it out and I looked URL dispatcher but couldn't find a solution to this.
When I click to change the page from navbar nothing happens. homepage is extends to the header.html but others are not. infact when I click to another button change the page it stays still. here are my codes;
home/first/views.py
from django.shortcuts import render
def index(request):
return render(request, 'first/home.html')
def contact(request):
return render(request, 'first/iletisim.html', {'content':['Eger bizimle iletisime gecmek isterseniz mail adresimizi kullanabilirsiniz.', 'gulumhali#outlook.com']})
def home (request):
return render(request, 'first/home.html')
home/first/urls.py
from django.conf.urls import url, include
from . import views
urlpatterns = [
url(r'^', views.index, name = 'index'),
url(r'^home/', views.index, name = 'home'),
url(r'^contact/', views.contact, name = 'iletisim'),
]
main urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('first.urls')),
url(r'^blog/', include('blog.urls')),
]
SOLUTION:
Like Adam said below; if you having this problem change the order home/first/urls.py for views.index.
Your first URL is catching everything. Essentially it's saying if the URL has a start to it, send to index.
Since it's the first url in the list, it's always going to catch that first since it goes through the url patterns and finds the first one it matches. As you've found out in the comment, if you move it to the bottom, when you go to either home/ or contact/ it will catch those first.
However, you still will have a problem with that because if you go to asdfblahblah/ or anything, it's still going to catch that. What you should do is add a $ to show that there should be nothing in the URL to route to index. That regex says if there is a start, and an end, and nothing in between, route to index.
urlpatterns = [
url(r'^$', views.index, name = 'index'),
url(r'^home/', views.index, name = 'home'),
url(r'^contact/', views.contact, name = 'iletisim'),
]
When I bring up 127.0.0.1:8000, the current page that show up is something.html template.
I would need to make it appear index.html at first launch, then when I click on other parts,it should go to 127.0.0.1:8000/something.html (or 127.0.0.1:8000/myapp/something.html).
What would be the structure to achieve this?
I frequently get error message : The current URL didn't match any of these.
Currently, my structure is
project
---myapp
---admin.py
---models.py
---url.py
---views.py
---static
---templates
---myapp
---html files
---mysite
---settings.py
--- url.py
under my settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
under mysettings/url.py
urlpatterns = [
# Examples:
url(r'^$', 'mysite.views.home', name='home'),
url(r'^myapp/', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls)),
]
mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.conf.urls import patterns
from django.views.generic import TemplateView
urlpatterns = [
# Examples:
url(r'^', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls)),
]
admin.site.site_header = 'Admin'
myapp/url.py
from . import views
from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView
urlpatterns = patterns('',
#brings up something.html when we comment it out. No module found if included.
#url(r'^$', views.home, name='home'),
url(r'^$', views.post_list, name='post_list'),
)
Include urls of your myapp app in mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
urlpatterns = [
url(r'^', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls)),
]
Now, all urls, starting by 127.0.0.1:8000, will check if there is a view that can handle the request. I recommend you to read this to understand how Django URL dispatcher works : [Django documentation - URL Dispatcher] (https://docs.djangoproject.com/en/1.8/topics/http/urls/).
2. Add new route in your myapp.urls:
from django.conf.urls import url, patterns
from . import views
urlpatterns = patterns('',
url(r'^$', views.home, name='home'),
url(r'^something$', views.something, name='something'),
url(r'^posts$', views.post_list, name='post_list'),
)
Now :
127.0.0.1:8000/ will executed code of views.home
127.0.0.1:8000/something will executed code of views.something
127.0.0.1:8000/posts will executed code of views.post_list
Let's define these view now
3: Define the views in myapp.views :
from django.shortcuts import render
def home(request):
"""
Return home page
"""
return render(request, 'myapp/home.html')
def something(request):
"""
Return something page
"""
return render(request, 'myapp/something.html')
def post_list(request):
"""
Return something page
"""
# do what you want
Add your templates in myapp/templates/myapp/. Add home.html and something.html.
Now forget, the `.html
You create a url (with attached view to it).
In the view you render any html page you want.
If you use function based views your 'mysite.views.home' may look like:
def home(request):
...
return render(request, 'path/to/index.html')
and so on.
It's the basics so I won't talk about this much. You can find a good tutorial about mapping urls to views there.
I'm having the 404 error when trying to handle a view that is not related to the main page. For example, if I initially start at the main page home, and want to navigate to another page called, otherpage, I receive a 404 otherpage.html not found.
The way I'm doing is based off intuition. So if there's a better way to do this, please mention it:
in the file:
prd/
views.py
url.py
otherstuffthatshouldbehere.py..
I have views.py (this is where I think the error is):
from django.shortcuts import render
def home(request):
context = {}
template = "index.html"
return render(request, template, context)
def otherpage(request):
context = {}
template = "otherpage.html"
return render(request, template, context)
Then urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^$', 'prd.views.home', name='home'),
url(r'^$', 'prd.views.otherpage', name='otherpage'),
url(r'^admin/', include(admin.site.urls)),
)
This returns a 404 for the otherpage.html. I have the template directory working fine. But how do I go about handling multiple views?
EDIT:
Upon adding:
url(r'^otherpage$', 'prd.views.otherpage', name='otherpage'),
I received this error:
Using the URLconf defined in prd.urls, Django tried these URL patterns, in this order:
^$ [name='home']
^about$ [name='about']
^projects$ [name='projects']
^admin/
The current URL, about.html, didn't match any of these.
The urlpatterns starts at the top and then goes down until it matches a URL based on the regex. In order for Django to serve up the page located at otherpage.html there has to be a URL defined in urlpatterns that matches otherpage.html otherwise you will get the 404.
In this case you have:
url(r'^$', 'prd.views.home', name='home'),
url(r'^$', 'prd.views.otherpage', name='otherpage'),
Note that nothing will ever get to otherpage here because home has the same pattern (regex) and will therefore always match first. You want to have a different URL for both those so that you can differentiate between them. Perhaps you could do something like:
url(r'^$', 'prd.views.home', name='home'),
url(r'^otherpage.html$', 'prd.views.otherpage', name='otherpage'),
After making this change you now have a match for otherpage and hopefully no more 404.
EDIT:
url(r'^otherpage.html$', 'prd.views.otherpage', name='otherpage'),
This matches www.example.com/otherpage.html but it will not match www.example.com/otherpage
To match www.example.com/otherpage you need to use:
url(r'^otherpage$', 'prd.views.otherpage', name='otherpage'),
Note the difference in the regex, there's no .html here. The regex matches exactly what you put in it.
I am pretty new to Django and when I laid my site out I did it in the following way:
The project is a "portal" of sorts,
App 1 "home" is the app that houses the homepage, login, registration
and other "site-wide" functionality
App 2 "inventory" is a basic asset inventory
App 3 "dashboard" is a set of status dashboards based on assets in the inventory
Right now I am trying to add the login functionality and I have a main project urls.py that looks like this:
File: /portal/urls.py
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', include('home.urls'), name='home'),
url(r'^admin/', include(admin.site.urls), name='admin'),
url(r'^inventory/', include('inventory.urls'), name='inventory'),
url(r'^dashboard/', include('dashboard.urls'), name='dashboard'),
url(r'^capacity/', include('capacity.urls'), name='capacity'),
)
My plan is to use the inclusion of .../home/urls.py to manage any site-wide functionality such as logging in, registering, etc.
Right now the home index view shows just fine, but anything else in .../home/urls.py gives me a 404
File: /home/urls.py
from django.conf.urls import patterns, url
from home import views
urlpatterns = patterns('',
url(r'^test/$', views.index, name='home_test'),
url(r'^ajax_login/$', views.ajax_login, name='ajax_login'),
url(r'^$', views.index, name='home_index'),
)
At this point I suppose my question is two-fold: Am I approaching this correctly? If so, how can I get the desired functionality? If not, how should I change the way things are set up/laid out to do it the correct "best practices" way?
Thanks in advance!
EDIT
Got it working thanks to dt0xff and holdenweb, accepted holdenweb's answer since it was more accurate including the need to put the home url inclusion below the rest.
Here is my new portal/urls.py file for reference
File: /portal/urls.py
from django.conf import settings
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls), name='admin'),
url(r'^inventory/', include('inventory.urls'), name='inventory'),
url(r'^dashboard/', include('dashboard.urls'), name='dashboard'),
url(r'^capacity/', include('capacity.urls'), name='capacity'),
url(r'^', include('home.urls'), name='home'),
)
The issue is with your first pattern, which will only match the empty URL. So any empty URL will cause the home/urls.py URLs to be analyzed, but only the empty one will match even in that.
Unfortunately if there is no common prefix then that pattern "^" will match every URL (since they all start at the beginning ...).
So you should either use a common prefix for all the pages, or put the home URL specification at the end to give the other URLs a chance to match before it is tested.
Django looks in urls like this:
Get list of root urls - portal/urls
Find first, which matches to current url
If it is include, go to included urls, cutting off matched part
Your problem is here
url(r'^$', include('home.urls'), name='home'),
I mean, here
'^$'
You want url to match "start and then end of url". It works good with root(dunno.com/), but not with others, because url will contain something more...
So, just remove $ and you are fine
url(r'^', include('home.urls'), name='home'),
you never need to add regex($) at project/urls.py