I want to create urls to go to links like this: examples.com/baiviet/post-example/
*post-example is a slug
this is my root urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$', include('blog.urls')),
)
Then, this is my blog/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from blog import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^baiviet/(?P<slug>)/$', views.view_post, name='viewpost'),
)
My views.py:
def view_post(request, slug):
getpost = get_object_or_404(Blog, slug=slug)
return render(request, 'view_post.html', {'post':getpost})
And my view_post.html:
{{ post.content }}
The only thing I have is "Page Not Found" Error. I have tried to solve it and it takes me 2 hours before posting this question. I hope that someone can help me solve this problem. Thanks you
The reason for 404 is that in your root urlconf, you have
url(r'^$', include('blog.urls'))
Here, $ indicates end of url pattern. Change that to
url(r'^/', include('blog.urls'))
# ^ note the $ shoudl be replaced by / when you are doing an include.
Here is the relevant documentation:
Note that the regular expressions in this example don’t have a $ (end-of-string match character) but do include a trailing slash. Whenever Django encounters include() (django.conf.urls.include()), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.
The issue with the missing pattern, as alecxe mentions on <slug> would arise after it resolves this issue (404).
EDIT:
For you to access the homepage, you need to have a trailing / or have the setting APPEND_SLASH setting to True. Since your URL Pattern is expecting a prefix of / - Now, if you dont want it, in your root urlconf, change r'^/' to just r'^'
You have an empty capturing group configured for the url:
url(r'^baiviet/(?P<slug>)/$', views.view_post, name='viewpost')
HERE ^
You need to provide a pattern for a slug to match, e.g. alphanumeric, underscore and a dash:
url(r'^baiviet/(?P<slug>[a-zA-Z0-9_-]+)/$', views.view_post, name='viewpost')
Related
I am going throught the Django tutorial here:
https://docs.djangoproject.com/en/1.10/intro/tutorial01/
I follow the instructions exactly. But when I try to go to http://localhost:8000/polls/, I don't see the message “Hello, world. You’re at the polls index.” as expected. Instead, I get the following error:
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, polls/, didn't match any of these.
Here is my mysite/urls.py file. I am not sure why the first regex pattern in urlpatterns is not recognized.
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
You might have forgot .html extension in views.py while requesting the page.
This is driving me crazy. Everything looks good. I am getting this error:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/home
Using the URLconf defined in gds.urls, Django tried these URL patterns, in this order:
^admin/
^fixiss/
The current URL, home, didn't match any of these.
Here is my root url:
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^fixiss/', include('fixiss.urls')),
]
My app url:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.home, name="index"),
]
And the view in my app:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request):
return HttpResponse("Home page!")
Any help would be greatly appreciated!
Assuming your "app url" module is 'fixiss.urls' where you only have one pattern (the empty string) and you are you are including it under fixiss/, the only match should be:
http://localhost:8000/fixiss/
If you change your one pattern to:
url(r'^home$', views.home, name="index")
that view will be served under
http://localhost:8000/fixiss/home/
The actual name of the view function (home in this case) is rather irrelevant when it comes to url pattern matching. What counts is the specified regex pattern.
This is very well documented:
Django url dispatching in general
Including urls in particular
That is because no url matches home/. Your url should be http://localhost:8000/fixiss/
In the app's (fixiss) url file, the regex is empty meaning, it does not expect a string after fixiss/ in the url for it to match.
My view's function are not called by urls.py patterns. I can only call them explicitly.
Basic Layout is
--project
----persons
----project
project/urls.py is:
from django.conf.urls import url, include, patterns
from .views import page #irrelevant
# from persons import views as person_views
urlpatterns = patterns('',
url(r'^(?P<slug>[\w./-]+)/$', page, name='page'), #irrelevant
url(r'^$', page, name='homepage'), #irrelevant
url(r'^persons/', include('persons.urls', namespace='persons')), # WORKS
# url(r'^persons/$', person_views.persons, name='persons'), #wont work
# url(r'^persons/add/$', person_views.add_person, name='add_person'), #wont work
)
Everything is ok until this point, since persons.urls is included successfully... But inside:
persons/urls.py:
from django.conf.urls import patterns, url
#from persons.views import index_persons, add_person
from persons import views
#views.index_persons('GET / HTTP/1.0') # >>> WORKS - function called <<< !!!
urlpatterns = patterns('',
url(r'.', views.index_persons, name='index_persons'), # DOES NOT WORK
url(r'^add/', views.add_person, name='add_person'), # DOES NOT WORK
)
I have also tried other regex like:
url(r'*', views.index_persons, name='index_persons'), # DOES NOT WORK
url(r'^$', views.index_persons, name='index_persons'), # DOES NOT WORK
no luck...
My persons/views.py file contains:
def index_persons(request):
print 'WHY???'
def add_person(request):
print 'WHY???'
'WHY???' is normally printed in the console (stdout - since I execute from manage.py runserver), when the index_persons function is called explicitly from persons/urls.py
Any thoughts?
In project/urls.py, move the page url pattern below the other ones. Otherwise, a request to /persons/ will be matched by the page url pattern first.
url(r'^$', page, name='homepage'),
url(r'^persons/', include('persons.urls', namespace='persons')),
url(r'^(?P<slug>[\w./-]+)/$', page, name='page'),
Inside persons/urls.py, you should have:
urlpatterns = patterns('',
url(r'^$', views.index_persons, name='index_persons'),
url(r'^add/$', views.add_person, name='add_person'),
)
Your url rule should be
url(r'^/?$, views.index_persons, name='index_persons'),
NOTE 1: Don't forget to restart the server.
NOTE 2: namespace='persons' is equal to not set namespace, because your url is persons/ is the same.
I am trying to find a way to display my django login page when the user ask for an unwanted url? Which syntax should I user ?
As of today I have
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
# Examples:
url(r'^login' , 'database.views.index', name='login'),
url(r'^create-user/' , 'database.views.account_creation', name='create_user'),
url(r'^get-details/' , 'database.views.get_details', name='get-details'),
url(r'^upload-csv' , 'database.views.upload_csv', name='upload_csv'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
#url(r'^' , 'database.views.index', name='login'),
)
I would like that if a user ask for a crazy url, it would be directed to the login url (ie view.index function).
Any idea ?
Without commenting on whether you should do this, Django will attempt to match your url patterns in order. So if you want a fall-through / catch-all handler, put this last:
url(r'^.*', 'database.views.index', name='unmatched')
Hello I'm a newbie in Django.
I'm creating a blog app for practice and I wanted to separate the urls that relates to the blog application from the urls that relates to the other applications.
Since there are many blog-related url patterns, I just included in the main urls.py.
Here is my urls.py:
My_Project/My_Project/urls.py
urlpatterns = patterns('',
# Blog
url(r'^$', include('app_blog.urls'), name='app_blog'),
# Admin
url(r'^admin/$', include(admin.site.urls), name='admin_page'),
......
My_Project/app_blog/urls.py
urlpatterns = patterns('',
# Index page
url(r'^index/$', index_page),
# User page
url(r'^user/(?P<pk>\d+)/', UserDetail.as_view(), name='user_detail'),
......
So I expected that when I navigate to "www.example.com/index" the browser would show the index_page view and for "www.example.com/user/1", it will show the user detail view for user with id equal to 1.
For some reason, however, it shows the 404 page not found error for both pages.
Where have I gone wrong?
name parameter is for a single url pattern. Remove it from this line in My_Project/My_Project/urls.py
The regex pattern should not be '^$'. Instead just plain ''.
url(r'', include('app_blog.urls'))
The problem is here:
# Blog
url(r'^$', include('app_blog.urls'), name='app_blog'),
~~~
You are restricting your url to an empty path, that means everything that is not an empty path will not be matched against app_blog.urls.
Do this instead:
# Blog
url(r'^', include('app_blog.urls')),