I simply want to get to another directory than index.
What I think Django is doing after I type in "localhost:8000/ysynch/":
Checking "ysynch/urls.py" (my root urls.py file)
Finding "ytlinks/" including "links.urls"
Matching with "ytlinks/" (in file "links.urls") and calling "views.ytlinks"
But instead, views.index is called. Where am I doing a mistake?
root\urls.py
C:\Users\xyron\Desktop\ysynch\ysynch\urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^ytlinks/', include('links.urls')),
url(r'^$', include('links.urls')),
]
links\urls.py
C:\Users\xyron\Desktop\ysynch\links\urls.py
urlpatterns = [
url(r'^ytlinks/', views.ytlinks, name='ytlinks'),
url(r'^$', views.index, name='index'),
]
Because paths are added to inclusion
url(r'^ytlinks/', include('links.urls')),
so you have following
# /ytlinks/ytlinks
url(r'^ytlinks/', views.ytlinks, name='ytlinks'),
# /ytlinks/
url(r'^$', views.index, name='index'),
so when you call /ytlinks/ you get to the view inside ytlinks which is index view again
the view you want to present is /ytlinks/ytlinks/
Checking "ysynch/urls.py" (my root urls.py file)
Finding "ytlinks/" including "links.urls"
Matching with "ytlinks/" (in file "links.urls") and calling "views.ytlinks"
The already matched part of the url will be excluded from further matching inside your includes.
So you basicly try to match "ytlinks/" again which would be true for "ytlinks/ytlinks/". You just want to match like this in your links\urls.py:
urlpatterns = [
url(r'^$', views.ytlinks, name='ytlinks'),
]
All of your urls in this file already match the first part "ytlinks/" and you only have to match the rest which is nothing or ^$ in your case.
The main part of your confusion is that you are expecting each urls.py to be trying to match the entire url, but instead you can think of include as doing a string concatenation and joining the previous parts of the url with the section of the url in the next urls.py
When django tries to match a url, it goes down the list of regexes until it finds one that matches so what you have is the following
r'^ytlinks/' + r'^ytlinks/' ==> views.ytlinks
r'^ytlinks/' + r'^$' ==> views.index
r'^$' + r'^ytlinks/' ==> views.ytlinks (not quite!)
r'^$' + r'^$' ==> views.index(not quite!)
So the first one of those that gets matched would be the second one for your url. $ in regex means end of a string so it won't bother to check anything that follows it here so you can rule out the last two regexes.
So your fix is threefold,
The links/urls you need to remove the first regex
url(r'^ytlinks/', views.ytlinks, name='ytlinks'),
You need to remove the $ from your inclusion url in the other urls.py
url(r'^', include('links.urls')),
You need to modify the views that each link should go to so eventually you end up with the following
root\urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^ytlinks/', include('links.urls')),
url(r'^', views.index, name='index'),
]
links\urls.py
urlpatterns = [
url(r'^$', views.ytlinks, name='ytlinks'),
]
Related
i want to try another apps in django but i got problem when access another apps.
Page not found
tree:
main:
search:
index.html
scrape.html
preprocessing:
index.html
the scenario like this. from scrape.html i want to access index.html in preprocessing but got an error path not found. I've done to add apps in settings.py
main url.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('search.urls')),
path('preprocessing/', include('preprocessing.urls')),
]
search url.py
urlpatterns = [
path('', views.index,),
path('scrape/',views.scrape,),
]
slice of scrape.html:
Preprocessing
preprocessing url.py
path('', views.index),
let me know where did I go wrong, thx for your help
Your anchor tag is written as:
<a href = "/preprocessing" ...>
The problem here is that your url pattern is like 'preprocessing/', notice that it ends in a trailing slash while your anchors url doesn't. Furthermore in your settings you set APPEND_SLASH = False.
Normally when Django finds a url not ending in a trailing slash it appends a slash to it and redirects the user to this new url. But you have stopped this behaviour by setting APPEND_SLASH = False. As the first step I would advice you to change this back to APPEND_SLASH = True.
Next you should always name your urls and use those names to refer to the urls. So your urlpatterns should be:
main url.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('search.urls')),
path('preprocessing/', include('preprocessing.urls')),
]
search url.py
urlpatterns = [
path('', views.index, name='index'),
path('scrape/',views.scrape, name='scrape'),
]
preprocessing url.py
path('', views.index, name='preprocessing'),
Now in your templates you would simply use the url template tag:
Preprocessing
I'm trying to employ positional parameters on a view inside one app of my django application.
The app is called member. and the member/urls.py is called by the project trekfed:
trekfed.py
from member import urls as mviews
from public import views as pviews
urlpatterns = [
url(r'^member/', include(mviews)),
url(r'^admin/', admin.site.urls),
member\urls.py
urlpatterns = [
url(r'$', views.index, name='myprofile'),
url(r'^(?P<mbr>)/$', views.index, name='profile'),
url(r'^api/', include(router.urls)),
]
views.py
#login_required
def index(request, mbr=""):
print(mbr)
data = {}
if mbr:
user = User.objects.filter(Q(id=mbr)|Q(username=mbr)).values('User_id')
else:
user = request.user
data['user'] = user
data['member'] = models.Member.objects.get(User=user)
data['Address'] = models.Address.objects.filter(User=user).get(Primary=True)
data['Phone'] = models.Phone.objects.filter(User=user).get(Primary=True)
data['Family'] = models.Family.objects.filter(Primary=user.member)
data['Sponsor'] = models.Family.objects.filter(Dependent=user.member.pk)
data['rank'] = models.Promotion.objects.filter(User=user).latest('Date_Effective')
return render(request, "member/profile/page_user_profile.html", data)
when authenticated, if I go to http://localhost:8000/member/ I can see my profile. No problems.
If I go to http://localhost:8000/member/user2/ I still see my profile, not user2's.
Is there something that I'm missing here? Thanks.
Update 1
Tried:
url(r'^(?P<mbr>[a-zA-Z0-9]+)/$', views.index, name='profile'),
and
url(r'^(?P<mbr>.+)/$', views.index, name='profile'),
with no change.
Yes there is something you're missing here and it's called regex pattern.
In your urls the url(r'^(?P<mbr>)/$'), pattern does not matches anything (it's just an empty string, ''). You should first think of what pattern you want to capture (say only words, only digits, both words and digits, both word and digits and -, both words and digits and - and _ etc.
It all depends on the captured pattern. Take a look at here for some common url patterns and whatever you choose, place it after the > character (url(r'^(?P<mbr>regex_pattern_here)/$'),).
If you want to make it an optional field then you still have to enter a regex pattern (in case something matches) and leave your urls as is:
urlpatterns = [
url(r'^$', views.index, name='myprofile'),
url(r'^(?P<mbr>regex_pattern_here)/$', views.index, name='profile'),
url(r'^api/', include(router.urls)),
]
With this approach, both http://localhost:8000/member/ (without an mbr) and http://localhost:8000/member/user2/ (with mbr = user2) will hit the views.index view.
[BONUS]: You can test your regex patterns in http://pythex.org/
I've been making some URLs in Django, including them and so on. Everything looks fine, but no matter what i do i always end up with a 404 on some of the simplest URLs.
For example I can browse myapp/0 abd myapp/1/details/, but i get 404'd at myapp/foo
So here are my urlconf :
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include('myapp.urls')),
]
and myapp urlconf :
urlpatterns = [
url(r'^foo/$ ', FooView.as_view()),
url(r'^(?P<bar_id>\d+)/$', BarByIdView.as_view()),
url(r'^(?P<bar_id>\d+)/details/$', BarDetailsByIdView.as_view()),
]
And when i tryp myapp/foo Django shows me the following urls list :
^admin/
^myapp/ ^foo/$
^myapp/ ^(?P<bar_id>\d+)/$
^myapp/ ^(?P<bar_id>\d+)/details/$
In the myapp.conf you have added a / at the end of the url pattern
url(r'^foo/$ ', FooView.as_view()
This must be viewed with /myapp/foo/ and not /myapp/foo because the first one matches the regex where as the second one won't.
Your URL is /myapp/foo/, not /myapp/foo. Note the trailing slash.
If you want both to work, ensure you have the APPEND_SLASH setting set to True and the CommonMiddleware enabled.
I've encountered a one nasty bug in my code while developing a personal blog in Django. Basically, I've changed my urls.py file by adding a couple of rules to make certain views accessible.
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from blog import views
urlpatterns = [
# Examples:
# url(r'^$', 'blogas.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', views.index, name='index'),
url(r'^(?P<slug>\w+)', views.view_post, name='view_blog_post'),
url(r'^about/$', views.about, name='about'),
url(r'^posts/$', views.posts, name='posts'),
]
Everything seems to be working except when I try access http://127.0.0.1:8000/about or /posts, Django throws out a 404 error. What is the reason of this? I've defined both rules but the system seems not to recognize the pattern - maybe I've mispelled something? Maybe I know nothing about url formatting (could be, it's my first time doing this stuff)?
A big thanks from a newbie programmer to everyone who finds the bug :)
The url-patterns are processed from top to bottom. Your third pattern ^(?P<slug>\w+) consumes everything, so about and posts is never reached.
An example: Django wants to find the view for the url about/. The patterns ^admin/ and ^$ do not match. But ^(?P<slug>\w+) does, because about starts with letters or numbers (the character sets contained in \w)
>>> import re
>>> re.search('^(?P<slug>\w+)', 'about/')
<_sre.SRE_Match object at 0x10b5b7be8>
So Django found a match, callBs views.view_post and finishes the request. That means, the more specific rule must come first. Better: avoid ambiguity.
You have to change position of url(r'^(?P<slug>\w+)', views.view_post, name='view_blog_post'),.
urlpatterns = [
# Examples:
# url(r'^$', 'blogas.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^$', views.index, name='index'),
url(r'^about/$', views.about, name='about'),
url(r'^posts/$', views.posts, name='posts'),
url(r'^(?P<slug>\w+)', views.view_post, name='view_blog_post'),
]
urlpatterns is list and order of urls is important.
I have an app, which I want to be the root of the website. So, I included the following urlpattern in my main urls.py like so:
(r'^$', include('search.urls')),
In my search urls.py, I have it set up in the following way:
urlpatterns = patterns('search.views',
(r'^$', 'index'),
(r'^results/$', 'results'),
)
The index urlpattern works, it goes to the index view, but the results urlpattern does not work. I get the following error when I try to access results:
Using the URLconf defined in textbook.urls, Django tried these URL patterns, in this order:
1. ^books/
2. ^$
3. ^admin/
4. ^static/(?P.*)$
The current URL, results/, didn't match any of these.
Edit:
main urls.py:
urlpatterns = patterns('',
(r'^books/', include('books.urls')),
(r'^$', include('search.urls')),
(r'^admin/', include(admin.site.urls)),
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
)
Try this(put the searchurl include at the end and remove the $):
urlpatterns = patterns('',
(r'^books/', include('books.urls')),
(r'^admin/', include(admin.site.urls)),
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT}),
(r'^', include('search.urls')),
)
The reason for this is that the search urls will only be included if your path matches ^$, this means that only the / url will match this pattern since $ marks the end of the pattern. The last line of my urlconf says: forward everything else to the search urlconf (notice the absence of $). Note, anything you place after this line will never be matched, since this line matches anything.