Django- Reversing custom Admin URLs - python

I'm using an App that configures some Admin URLs as:
#admin.py....
def get_urls(self):
urls = super(FormAdmin, self).get_urls()
extra_urls = [
url("^(?P<form_id>\d+)/entries/$",
self.admin_site.admin_view(self.entries_view),
name="form_entries"),
#......
]
return extra_urls + urls
I'm having trouble using template tags to get a URL corresponding to that, in one of my templates. I'm trying stuff like:
4-Entries
(Where forms is the App's label). I keep running into the No Reverse Match type of errors:
NoReverseMatch at /polls/ Reverse for 'forms_form_entries' with
arguments '(4,)' and keyword arguments '{}' not found. 0 pattern(s)
tried: []
What am I missing that makes the tag work correctly?

Try this in html
4-Entries

Your url pattern name form_entries doesn't match forms_form_entries from the URL tag. Django does not automatically prefix the pattern name with <app_name>_ as you appear to be expecting.
Change one of them so that they match.

Related

Django optional parameter is not readed from url

I was reading the thread Django optional url parameters
And following the steps to generate a URL with a single optional parameter.
Well, my URL should be:
/client/
/client/?clientname=John
And I have defined two urlpatterns
url(r'^$', views.index, name='index'),
url(r'^/(?P<clientname>\d+)/',views.index),
Well, at this point both of them render the page.
But, in my view:
def index(request, clientname='noparameter'):
print("The searched name is: " + str(clientname))
The searched name is always noparameter
Am I doing something wrong?
Url you are having is
/client/John/
instead of
/client/?clientname=John
also even in the following example using John will fail as your regex is for digits , check out more on topic of django dispatcher
/client/4/
if you want to get GET parameters instead you can do that in view by using the following
request.GET.get('clientanme', None)
It seems as though you are getting confused between a keyword argument and a get request. Using keyword arguments, which your urls.py is configured for, your view would like this:
def index(request, **kwargs):
clientname = kwargs.get("clientname", "noparameter")
print("The searched name is: " + str(clientname))
Your urls.py would also have to change to this for the url to this:
url(r'^client/(?P<clientname>\w+)/',views.index),
This could be called in the browser like:
/client/John

Error redirecting after login

I am trying to redirect users to a create-profile form after signup.Since I don't know how to use dynamic url for LOGIN_REDIRECT_URL.I tried this little trick of linking it to a static view then finally to a dynamic view using redirect.It gives the error Reverse for 'add_profile' with arguments '()' and keyword arguments '{'username': u'badguy'}' not found. 0 pattern(s) tried: [] (with 'badguy' truly the username of the just signed-up user').From the error,it is clearly passing the username to the view.My codes are:
settings.py
LOGIN_REDIRECT_URL = '/prof-change/gdyu734648dgey83y37gyyeyu8g/'
urls.py
url(r'^prof-change/gdyu734648dgey83y37gyyeyu8g/',views.login_redir,name='login_redir'),
url(r'^create-profile/(?P<username>\w+)/$',views.add_profile,name='create-profile'),
views.py
def login_redir(request):
user=get_object_or_404(User,username=request.user.username)
username=user.username
return redirect('add_profile',username=username)
def add_profile(request,username):
userman=User.objects.get(username=username)
........
........
When you reverse urls (including when you use a url name with the redirect shortcut), you should use the name of the url pattern, not the view. The url pattern has name='create-profile', so use that.
redirect('create-profile', username=username)

Reverse url lookup with include parameter

Does anyone know how to make a reverse url lookup on a url with an include parameter? For example I have the following url:
url(r'^photos/', include('photos.urls'), name="photos"),
And in my view i want to access it as follows
def photos_redirect(request, path):
return HttpResponsePermanentRedirect(reverse('photos') + path)
But it get the following error:
Reverse for 'photos' with arguments '()' and keyword arguments '{}' not found.
You have to reverse to a single urlpattern. The photos URL pattern is including all the urlpatterns in photos.urls, so you have to choose which single URL you are wanting to direct to.

Incorrect Django URL pattern match for 2 views with the same URL structure?

I have two url patterns in Django:
urlpatterns += patterns('',
url(r'^(?P<song_name>.+)-(?P<dj_slug>.+)-(?P<song_id>.+)/$', songs.dj_song, name='dj_song'),
url(r'^(?P<song_name>.+)-(?P<artist_slug>.+)-(?P<song_id>.+)/$', songs.trending_song, name='trending_song'),
)
When I visit a URL of the first pattern, it opens it correctly. However if I try and visit a URL of the second pattern, it tries to access the first view again. The variables song_name, dj_slug, artist_slugare strings and song_id is an integer.
What should be the URL patterns for such a case with similar URL structure?
Both urls use the same regex. I removed the group names and get:
url(r'^(.+)-(.+)-(.+)/$', songs.dj_song, name='dj_song'),
url(r'^(.+)-(.+)-(.+)/$', songs.trending_song, name='trending_song'),
Of course django uses the first match.
You should use different urls for different views. For example add the prefix to the second url:
url(r'^trending/(?P<song_name>.+)-(?P<artist_slug>.+)-(?P<song_id>.+)/$',
songs.trending_song, name='trending_song'),

django named url not picking variables

I have the following url pattern -
url(r'^detail/(?P<var>[\w]+)/(?P<var_slug>[\w-])/$', 'player_detail', name='player_detail'),
In my view, I have the following -
model_dict = {"player":PlayerProfile, "event":PlayerEvent, "need":PlayerNeed}
def player_list(request, var=None, var_slug=None):
'''
displays the list of vars
'''
objs = model_dict.get(var).objects.filter(slug=var_slug).order_by('-creation_time')[:20]
template_name = "list_"+str(var)+"s.html"
return render(request, template_name, {"objs":objs})
In my templates I finally do the following -
details of Player
The error I get is following -
Reverse for 'player_detail' with arguments '()' and keyword arguments '{u'var': u'baseball', u'slug': u'obj.slug'}' not found.
What am I missing?
also, is it a good way to pick models dynamically depending on the variable in the parameter, and generating a template name on the fly?
slug='obj.slug' should be slug=obj.slug
Your regex doesn't match a .
Also you probably want[\w-]+ just like your other regex.. one or more [\w or -]

Categories