Specifying a new template for Django Model - django

I have been looking around the web for a solution but I cant seem to find a valid solution.
I am trying to override the default "index" page for a specific model. I know you can change the URL setting in the project's url.py folder like this :
url(r'^items/(?P<itemId>\d+)/$', 'items.views.itemDetail')
While the above URL mapping works fine and the itemDetail(request) method gets called correctly, something like this :
url(r'^admin/homepage/$', 'homepage.views.index'),
doesn't seem to work. I know this doesn't work because of this line:
url(r'^admin/', include(admin.site.urls))
Every attempt at changing the above line's pattern causes errors unless I go directly to admin/homepage. So the default admin page dies. I am simply trying to change the default index template to something different. Short of changing the URL completely from XXX/admin/homepage to something else, I have no idea how to solve this.
Any help will be greatly appreciated.

I think it should just work as long your custom url definition comes before the one for the actual admin in the urlconf (urls.py):
urlpatterns = patterns('',
...
url(r'^admin/homepage/$', 'homepage.views.index'),
...
url(r'^admin/', include(admin.site.urls)),
...
)

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

Problem with i18n urls where I'm redirected to the right url but the template is not found

I read on the documentation page of i18n the following:
Warning
Ensure that you don’t have non-prefixed URL patterns that might collide with an automatically-dded language prefix.
I don't get this warning at all, what's the meaning of it??
I'm actually having a problem where I'm directed to my en/sitepages/news-events-listing or ar/sitepages/news-events-listing by a url defined like this in the root urls.py:
urlpatterns += i18n_patterns(
path("sitepages/", include("sitepages.urls")),
)
and I've put it before thr wagtail urls line, but the template returned by the view inside sitepages/views.py:
def sitepages_view(request):
template = loader.get_template('sitepages/news_events_page_listing.html')
context = {
'news_pages': NewsDetails.objects.all(),
'events_pages': EventsDetails.objects.all(),
'listing_page': NewsEventsListing.objects.first(),
}
return HttpResponse(template.render(context, request))
and the sitepages/urls.py:
app_name = "sitepages"
urlpatterns = [
path("news-events-listing/", sitepages_view, name='listing'),
]
I can't think of anything but this warning, and that I might need to understand it to solve the problem, although I know that the order of urls in the root urls.py matter, so I might include it if anyone's think it might cause the problem but I don't want to make the question too long
Note: if I replace the HttpResponse with a simple one like this HttpResponse('Hi') it works!
If your simple HttpResponse('Hi') works but your Response with the rendered template is not, this has nothing to do with your i18n prefixed URLs but with the loading of the template.
Make sure the template actually exists in PROJECT_ROOT/sitepages/templates/sitepages/news_events_page_listing.html' and sitepages is listed in your INSTALLED_APPS.
As an alternative, you could move the template to any app you've listed in your INSTALLED_APPS and make sure the path, starting from the templates/ directory of that app, matches the template name you use in your view.

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 urlconf sometimes failing?

I've got a django urls.py file like so:
Base file:
urlpatterns = patterns('',
(r'^', include('sp.sp_app.urls')),
(r'^', include('sp.sp_api_activity.urls')),
(r'^', include('sp.sp_api_player.urls')),
(r'^', include('sp.sp_web.urls')),
)
In the sp.sp_web.urls file, I have the following:
from django.conf.urls.defaults import *
urlpatterns = patterns('superproof.superproof_web.views',
(r'^$','index'), #Shows your home page
(r'^challenge$','spcreatechallenge'),
(r'^player/`$','getlastactivity'),
(r'^yearlysummary/','yearlysummary'),
(r'^processchallenge$','processchallenge'),
(r'^activity/(\w{32})$','activitydetail'),
)
The yearlysummary url is loading sometimes. Other times, I get a 404 error. This happens with the exact same valid URL. I don't change anything in the URL, or in the code.
When I get the 404 error with debug turned on, my yearlysummary URL pattern isn't on the list.
Any ideas?
A couple things I noticed that could be an issue...
I notice you are not namespacing your included urls for each app. Not knowing what the other urls modules look like, I can assume its possible that you can have colliding urls. I Would recommend doing something like this:
urlpatterns = patterns('',
(r'', include('sp.sp_app.urls')),
(r'^activity/', include('sp.sp_api_activity.urls')),
(r'^player/', include('sp.sp_api_player.urls')),
(r'^web/', include('sp.sp_web.urls')),
)
Normally when you have url includes, they are for different apps, so you would want to namespace them to avoid two apps specifying the same url pattern.
Also, whats that back-tick in one of your urls? Did you mean to expect that?
(r'^player/`$','getlastactivity'),
jdi is spot on, on the analysis. I'll try to explain why exactly is there the issue, in your case.
You will notice that if you include (r'^', include('sp.sp_web.urls') as the first pattern in the main urls.py, your view will rightly load always.
When you have it as the last pattern, the reason it doesn't match the other times is that, that pattern is matching some other pattern in the earlier pattern, say [/w+]. The debug page, on a 404 of the url pattern, displays all the sub-url patterns of one of the patterns it matches. This is exactly why the pattern isn't on the urls displayed on the debug page.
Like jdi mentions it is a good practice to namespace the urls properly, so this doesn't occur. You can probably do it even without namespacing, but your regexes have to be proper, with the end character $ included, at least.

django urlpatterns from sql

I am trying to create urlpatterns with sql query, but this will work only for those things that already was in sql table at the moment of server start. If it possible to make django to check for new urls dynamically from database?
I know, this can be done with regular expressions, but they are too greedy, I mean, that i need to make this at root level of my site and regexp will "eat" all matching names and this regexp must be the last of urlpatterns list.
Going on your comment to pyeleven's answer, it seems you have understood the point of urlpatterns. You don't need or want to specify the choices of your section in the urlconf. What you do is grab the value of each section of the url, and pass that as a parameter to the view. So, for example:
(r'^?P<section>\w+)/$', 'my_view')
This will grab urls like /name1/ and /name2/, and pass name1 and name2 to the view as the section parameter. So there's no need to change the code whenever you add a section.
Although this is the nastiest, most un-django-esque thing imaginable, you can get your urls from the db, if you really, really want to:
models.py:
from django.db import models
class Url(models.Model):
name = models.CharField(max_length=20)
urls.py:
from my_app.models import Url
urls = []
for url_object in Url.objects.all():
urls.append(url(url_object.name, 'my_view'))
urlpatterns = patterns('my_app.views', *urls)
Voilà. It actually works. Url patterns directly from the db. Please don't do this.
I'm going to go take a shower now.
Have you checked django flatpages?
http://docs.djangoproject.com/en/dev/ref/contrib/flatpages/?from=olddocs
Dynamic url might not be such a good idea, for example a bad url line added dynamically might make the server stop functioning.
Can you elaborate on your goals?