Django: Changing the index site? (home page) - django

The index site on my Django homepage stopped working because of a problem that will take a very long time to fix. The site can't be down for that long so I am trying to change the index site so that if you go to the primary url you will atleast end up on the website.
What I have done is change the urls.py file in the primary application, where I simply replaced the line
url(r'^', include('news.urls', namespace='news')),
to
url(r'^', include('events.urls', namespace='events')),
in the urlpatterns list, where news is the faulty page and events is the page that I want to be shown. However, after pushing this to live nothing changed, and for some reason my local Django development server is not working.
Did I do anything wrong, or is there anything else I have to do as well?
Thanks.

In the events app make sure you have a URL such as
......
url(r'^$', views.EventsIndex.as_view(), name='index'),
......

Replacing news.urls with events.urls may cause problems, if there are views/templates that try to reverse news urls. It would be better to leave the include as it is, and add a new URL pattern above that for the index:
from events.views import home
urlpatterns = [
url(r'^$', home, name='home'),
url(r'^', include('news.urls', namespace='news')),
...
]

Related

Django with Auth0

Currently learning about Auth0 and django.
https://github.com/auth0-blog/django-feed-auth0/blob/main/feed/feed/urls.py
I saw the URL pattern is like this
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('feedapp.urls')),
path('', include('social_django.urls')),
]
From what i learned previously we should have 1 path('', views.xyz) as it will be redundant to have same url pointing to the different views unless we have put other input like int or str. But auth0 have same path with other views.
Not really understand why is it okay for it to be like this? Hope you guys can explain to me.
Thanks
As explained in Django documentation, Django will parse through each URL pattern, in order, and stops at the first one that matches the requested URL.
As long as the path in "feedapp.urls" and "social_django.urls" are unique, everything will be fine. If some path are not unique, the first one found will be used. In your case, those in "feedapp.urls".
To ensure uniqueness, we can use named path to remove ambiguity Including other URLconfs.
Hope the explanation will help you

Django matching a url with paramater to start

I've got a site I'm working on where I need to match a parameter at the start
of the url:
test.com/the-data-slug
What is odd is I can't figure out why the following code dosen't work.
It dosen't match at all.
path('<slug:data-slug>/', DataLandingView.as_view(), name="data_landing")
I did get the following to work, but I know path is prefered over re_path.
re_path(r'^(?P<data-slug>\w+)/$', DataLandingView.as_view(), name="data_landing")
I'm really curious what I'm doing wrong with the path call, or is this just something path can't do?
Update:
Requested from commenters.
From urls.py these are the only url patterns in use.
urlpatterns = [
# Special Project level routing. Specifically routes to handle login/logout admin
path('admin/', admin.site.urls),
path('users/', include('django.contrib.auth.urls')),
path('accounts/', include('allauth.urls')),
# We are handling all convention level routing here as it makes more
# sence here as a master control router than in the individual projects
# Global Stuff for buisness
path('', HomePageView.as_view(), name='home'),
path('about/', AboutPageView.as_view(), name='about'),
# If we don't recoginze from the above we step down to the next layer
path('<slug:data-slug>', CLandingView.as_view(), name="c_landing")
]
Going to http://127.0.0.1:8000/abc-2020
and http://127.0.0.1:8000/abc-2020/
returns a 'Page Not Found' Exception
But if I use the following route with re_path. It works.
re_path(r'^(?P<c_name>[-\w]+)/$', CLandingView.as_view(), name="c_landing")

Django URLs inconsistent, and now returning 404

Previous working configuration of urls.py:
I'm not using include. Previously, a configuration of the project's url.py like this has worked perfectly for me:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'projectName.appName.views.view1'),
url(r'^about/$', 'projectName.appName.views.view2'),
url(r'^contact/$', 'projectName.give.appName.view3'),
url(r'^contact/thanks/$', 'projectName.appName.views.view4'),
)
Current configuration of urls.py on separate project:
However, I seem to be facing inconsistent results with this other configuration on another project.
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^foo/$', 'projectName.appName.views.view1'),
url(r'^foo/bar1/', 'projectName.appName.views.view2'),
url(r'^foo/bar2/', 'projectName.appName.views.view3'),
)
What's wrong:
For example, when I visit the URL at /foo/bar1, it inconsitently either brings me to the desired URL or loads up /foo/ instead while still having /foo/bar1 displayed in the URL bar in the browser.
/foo/bar2/ mostly (19 out of 20 times) returns a 404 when that URL is requested. Otherwise it shows /foo/ but the original URL remains in the browser. It just seems so weird and odd, and this has never happened to me in Django before.
If you noticed the missing $'s, I found out through experimentation that this configuration is slightly more reliable. With the $'s, all my URLs end up loading /foo/ while still showing the requested URL in the browser.
I guess that it's worthy to note that /foo/ and /foo/bar1/ were mostly working fine (but calling /foo/bar1/ in the browser would still load up /foo/ instead perhaps 1 out of every 20 times called or so) until I added /foo/bar2/.
How now brown cow:
I guess this would have to do with regexes, but I'm not sure how should I be handling regexes to make this work, other than to follow what I've done before that was right but now can't work. Yeah so I don't know how does regex work.
If someone could point me in the right direction or tell me where might I have gone wrong that would be very helpful. Thank you!
Other info:
I'm developing on Python 2.7, Django 1.3.7 on PythonAnywhere, not locally.

Django index url confusion

Hi thanks for looking into this.
I have been following Django's tutorial on URLs and got a bit confused/stuck on this part:
https://docs.djangoproject.com/en/1.4/intro/tutorial03/#decoupling-the-urlconfs
what I do not understand is if, say, on page mypage.com I provide only 2 possible URLs for mypage.com/polls and mypage.com/admin, what happens if the user goes to mypage.com? Obviously, I thought, the user will need to see some sort of 'welcome' page so I decided to add another URL to that urls.py:
urlpatterns = patterns('',
url(r'^/', 'myapp.views.welcome'), #when it's just mysite.com
url(r'^myapp/', include('myapp.urls')), #includes everything with mysite.com/myapp/...
url(r'^admin/', include(admin.site.urls)),
)
But then I get redirected to that welcome view from whichever page, whether i go to /myapp or not. So, I decided to create a views.py file outside myapps folder and put that welcome page there, and it seems to have worked, apart from that I get a 404.
I am so confused! Could you explain in lamers' terms please
Thanks,
blargie-bla
It should be
url(r'^$', 'myapp.views.welcome')
otherwise any URL will match the pattern. Django will call the view for the first pattern in urlpatterns that matches, so you need to be specific and include the end-of-the-line character ($) into the pattern.

Django admin fails when using includes in urlpatterns

I am trying to refactor out my application a little bit to keep it from getting too unwieldily. So I started to move some of the urlpatterns out to sub files as the documentation proposes.
Besides that fact that it just doesn't seem to be working (the items are not being rerouted) but when I go to the admin, it says that 'urlpatterns has not been defined'.
The urls.py I have at the root of my application is:
if settings.ENABLE_SSL:
urlpatterns = patterns('',
(r'^checkout/orderform/onepage/(\w*)/$','checkout.views.one_page_orderform',{'SSL':True},'commerce.checkout.views.single_product_orderform'),
)
else:
urlpatterns = patterns('',
(r'^checkout/orderform/onepage/(\w*)/$','commerce.checkout.views.single_product_orderform'),
)
urlpatterns+= patterns('',
(r'^$', 'alchemysites.views.route_to_home'),
(r'^%s/' % settings.DAJAXICE_MEDIA_PREFIX, include('dajaxice.urls')),
(r'^/checkout/', include('commerce.urls')),
(r'^/offers',include('commerce.urls')),
(r'^/order/',include('commerce.urls')),
(r'^admin/', include(admin.site.urls)),
(r'^accounts/login/$', login),
(r'^accounts/logout/$', logout),
(r'^(?P<path>.*)/$','alchemysites.views.get_path'),
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root':settings.MEDIA_ROOT}),
The urls I have moved out so far are the checkout/offers/order which are all subapps of 'commerce' where the urls.py for the apps are so to be clear.
/urls.py in questions (included here)
/commerce/urls.py where the urls.py I want to include is:
order_info = {
'queryset': Order.objects.all(),
}
urlpatterns+= patterns('',
(r'^offers/$','offers.views.start_offers'),
(r'^offers/([a-zA-Z0-9-]*)/order/(\d*)/add/([a-zA-Z0-9-]*)/(\w*)/next/([a-zA-Z0-9-)/$','offers.views.show_offer'),
(r'^reports/orders/$', list_detail.object_list,order_info),
)
and the applications offers lies under commerce.
And so the additional problem is that admin will not work at all, so I'm thinking because I killed it somewhere with my includes.
Things I have checked for:
Is the urlpatterns variable accidentally getting reset somewhere (i.e. urlpatterns = patterns, instead of urlpatterns+= patterns)
Are the patterns in commerce.urls valid (yes, when moved back to root they work).
So from there I am stumped. I can move everything back into the root, but was trying to get a little decoupled, not just for theoretical reason but for some short terms ones.
Lastly if I enter www.domainname/checkout/orderform/onepage/xxxjsd I get the correct page. However, entering www.domainname/checkout/ gets handled by the alchemysites.views.get_path.
If not the answer (because this is pretty darn specific), then is there a good way for troubleshoot urls.py? It seems to just be trial and error. Seems there should be some sort of parser that will tell you what your urlpatterns will do.
Adding the following line in my urls.py worked for me:
from django.conf.urls import include
Have a look at the django docs for including other url confs. I think you might have misunderstood them. In particular
Whenever Django encounters 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.
As an example, you have
(r'^/checkout/', include('commerce.urls')),
(r'^/offers',include('commerce.urls')),
(r'^/order/',include('commerce.urls')),
This means that
/checkout/offers/
/offers/offers/
/order/offers/
will all match the url pattern (r'^offers/$','offers.views.start_offers') in commerce/urls.py.
If you want to define a view for /checkout/ in commerce.py, you need to add the pattern
(r'^$', 'path_to_your_view')
because the /checkout/ part will be chopped off by the include()
As, an aside:
In /commerce/urls.py, use
urlpatterns = patterns('',
...
for the first patterns you define. You can then use urlpatterns += later in the same file.