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 -]
Related
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.
I'm trying to do a reverse like this:
print reverse("shows-view")
This is in my urls.py:
url(r'^shows/(\d+)$', views.show_details, name="shows-view"),
Whenever I try to do that, it just returns:
Reverse for 'shows-view' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['shows/(\\d+)$']
But if I try to access the page directly (http://localhost/shows/3333) then it works fine
But if I do a reverse for other views like this:
print reverse("shows-default-view")
with the following declaration in the same urls.py file:
url(r'^shows/', views.popular, name="shows-default-view"),
then it works fine. Does anyone have any idea why?
The URL in question accepts an argument (\d+) which you are not passing your reverse function. Just think: this is a details view, but which show do you want to display?
To fix, call reverse with the args parameter:
reverse("shows-default-view", args=[1]) # to show show with id of 1
In general for URL's like that, the recommendation is to have a named captured group:
url(r'^shows/(?P<pk>\d+)$', views.show_details, name="shows-view")
And then the call to reverse will be:
reverse("shows-default-view", kwargs={'pk': 1})
To use reverse in a template, just put the two arguments together:
{% url 'shows-view' 1 %}
Trying to access part of code by get request and receiving this error
NoReverseMatch: Reverse for 'homepage' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: [] looked around for help and i think its that am missing some arguments.
here is request am using in my test
req = self.client.get('/password/reset/')
and the code it leads to is as:
class PasswordResetEmailView(FormView):
template_name = 'temp/password_reset.html'
form_class = PasswordResetForm
success_url = 'password_reset_ok'
def get(self, request, *args, **kwargs):
form = self.get_form(self.form_class)
return self.render_to_response(self.get_context_data(form=form))
any guidance ?
You ask specifically about "determining which arguments are missing", but should be irrelevant to your error.
Your error message states that it can't find any URL patterns matching the name "homepage", before attempting any argument matching. Therefore no URLs exist named "homepage" in your setup. If it was an argument mismatch, you'd see a list of named URLs in the error such as "tried X urls."
Either define a URL named home in your URLConf url(r'^home/$', 'home_view', name="homepage") or find the location that is calling reverse('homepage') and remove it. You will find this information in your traceback.
What is wrong with my approach?
When I post new data, I want it to return back to the page with the input fileds empty. But it gives me this error
NoReverseMatch at /school/new-school/
Reverse for 'new-school' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
This is my model. Please note, reverse_lazy was imported
class SchoolList(models.Model):
name = models.CharField(max_length=15, null=False)
def __str__(self):
return '%s' % (self.name)
def get_absolute_url(self):
return reverse_lazy('new-school')
This is my url.py
url(r'^school-list/$', SchoolListtView.as_view(), name='school-list'),
url(r'^new-school/$', CreateSchoolListView.as_view(), name='new-school'),
url(r'^school(?P<pk>\d+)/update/$', SchoolListUpdate.as_view(), name='update-school')
This is my view for create.
class CreateSchoolListView(CreateView):
template_name = 'school\create_form.html'
model = SchoolList
fields = ['name']
This is how I specified the urls in the template.
Create New School
View all Schools
When the page is displayed, I can click the links and it will go to the correct pages. But when I post a data, the it throws the above error. I have been on this for hours and read many of the answers online. It seems mine is a unique case.
Try adding namespace to get_absolute_url().
def get_absolute_url(self):
return reverse_lazy('school:new-school')
Make sure you import your app's urls in your project's urls with namespace like:
url(r'^school/', include('school.urls', namespace="school"))
to use namespace in your templates like this:{% url 'school:new-school' %}
Or remove namespace:
url(r'^school/', include('school.urls'))
to use url without namespace in template:{% url 'new-school' %}
Using get_absolute_url would be a bad approach in this case, since it's an instance method, designed to get url for single model instance.
If you want to add method to model, you should use something like this:
#classmethod
def get_create_url(cls):
return reverse_lazy('school:new-school')
I have a problem with redirection in Django :
my view
def myP(request,namep):
return render(request,"site/myP.html")
def create(request):
nom="alaild"
....
return redirect(reverse(myP,namep=nom))
urls to this views
url(r'^create$', 'create', name='create'),
url(r'^myp/(?P<namep>\d+)','myP', name="myp"),
I have this error :
reverse() got an unexpected keyword argument 'name'
I want create view redirects to myP view but myP view have 1 argument and I don't know how make...
May be
reverse(myP, kwargs={'namep': nom})
In your view, nom is a string, but in your url pattern you are using \d+ (one or more digits).
A common approach is to accept a 'slug' which can contain letters, digits, underscores and hyphens:
url(r'^myp/(?P<namep>[\w-]+)','myP', name="myp"),
Then you need to fix the syntax of your reverse call. Either of the following should work.
reverse(myP, kwargs={'namep': nom})
reverse(myP, args=(nom,))