django template url from another app - django

I have to be missing something silly. I have a {% url %} in a template where the action is from another app. It isn't working but I have no clue if there is something different about using view functions from other apps or if I am just doing something silly.
call/template/call/file.html
<form action="{% url 'upload_image' %}"></form>
picture/urls.py
from .views import PictureList, PictureCreate, PictureDetail, PictureUpdate, PictureDelete, upload_image
...
url(r'^upload_image/$', upload_image, name='upload_image'),
...
picture/view.py
def upload_image( request ):
print 'IN IMAGE UPLOAD'
print request
All I ever get is:
NoReverseMatch at /call/4/
Reverse for 'upload_image' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

When calling reverse() on an URL that comes from a different application, you should use its "namespaced" version, like so:
{% url 'app_name:app_url' %}
In your specific case, that translates to:
{% url 'picture:upload_image' %}

Related

i am not able to render my urls in my template it gives me error rendering

NoReverseMatch at /
Reverse for 'Jersey' with no arguments not found. 1 pattern(s) tried: ['Jersey/(?P[^/]+)/$']
Below is the code to my views.py
class JerseyView(TemplateView):
#paginate_by=3
template_name='Ecommerce/Jersey.html'
def get_context_data(self, **kwargs):
et =super(JerseyView, self).get_context_data(**kwargs)
et['United']= Item.objects.filter(category="3").filter(subcategory="9")
et['Chelsea']= Item.objects.filter(category="3").filter(subcategory="10")
return et
below is the code for my urls.py
path('Jersey/<slug>/', JerseyView.as_view(), name="Jersey" ),
I called This link in my Navbar as
<a class="dropdown-item" href="{% url 'Ecommerce:Jersey' %}">Men's Clothing</a>
when I click on it it gives me the error as NoReverseMatch at / Reverse for 'Jersey' with no arguments not found. 1 pattern(s) tried: ['Jersey/(?P[^/]+)/$']
I don't know if there's something i am missing out because i have checked my spelling and still getting the same error
By using {% url 'Ecommerce:Jersey' %}, you're trying to access Jersey/<slug> path without giving any argument. So Django router expect a argument on this path (slug).
You need to provide it, (only you knows what slug goes there...) like this :
{% url 'Ecommerce:Jersey' Jersey.slug %}
is this slug dynamically change or not? if it is then first you should correct your url like this: 'Jersey/<slug:slug>/' and in href tag, get slug from context you passed in views.py like what user BriseBalloches said. but if your slug is a fixed parameter, you can just write it in href tag. suppose your slug parameter is country then your href tag should work with href="Jersey/country" or href="{% url 'Ecommerce:Jersey'country %}"
these href tags will generate http://127.0.0.1:8000/Jersey/country

Error during template rendering

i am trying to render the details page of the product by giving the url www.example.com/product_name/product_id. But i am getting this error.
Reverse for 'product_details' with arguments '(u'lehnga choli', 43)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['(?P[a-zA-Z]*)/(?P[0-9]+)/$']
here is my urls.py
url(r'^(?P<product_name>[a-zA-Z]*)/(?P<product_id>[0-9]+)/$', 'designer.views.product_details', name='product_details'),
and here is my urls in html template
{% url 'product_details' designs.name designs.id %}
and this is my views.py
def product_details(request, product_name, product_id):
design = Design.objects.get(id=product_id)
return render_to_response("designer/product_detail.html", {
"design":design,
"current": "product_detail",
}, context_instance=RequestContext(request))
There's a space in the name (which can't be used in a URL). As I mentioned in my comment, you might want to look into a SlugField
However, since you're looking up the Design by id in your view, it doesn't really matter whether the model has a slug. You can use the template tag slugify to just make it passable through the URL.
{% url 'product_details' designs.name|slugify designs.id %}
This does require a small tweak to your URL as well, because spaces are replaced with a - - and I just use \w in general.
url(r'^(?P<product_name>[\w-]+)/(?P<product_id>[0-9]+)/$', 'designer.views.product_details', name='product_details'),

How to reach url from form in django

I am new to Django and I have a simple question. Here is a view :
def watchmovie(request, idmovie):
[...]
return render(request, 'movies/watch_movie.html', locals())`
and I would like to create a simple form :
an IntegerField that would redirect to the correct url :
if I submit "42" it will redirect me to the view watchmovie with the parameter 42 as idmovie.
How can I do that?
I tried something like that
<form action="{% url "movies.views.watchmovie" %}" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
my url.py is
from django.conf.urls import patterns, url
urlpatterns = patterns(
'movies.views',
url(r'^movie/(?P<idmovie>\d+)$', 'watchmovie'),
)
and Django says
Reverse for 'movies.views.watchmovie' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['movies/movie/(?P<idmovie>\\d+)$']
Thank you!
The reason you are getting that error is because of a mistake in your url tag usage. Your watchmovie view url definition expects an argument to be supplied for idmovie. Since you are not supplying any argument in your url tag call, it looks only for urls which do not require an argument. Since there is none, you get an error.
But that is just a symptom. The real issue is that the way you have this structured there is no view listening for a post from your form.
The easier way to structure this is to use the same view to both display the form and to play the movie. If your view is hit with a GET request, display the form. If it is hit with a POST, validate the form (which will contain the movie id) and then respond with the page that plays the movie. That way there is no need to pass idmovie within your url.. you can remove that from your url definition and also remove the need to specify the action= attribute in your tag.. it will just post right back to where it came from.

Django: Reverse for 'booking' with arguments '()' and keyword arguments '{}' not found

Sorry if this looks like a duplicate question, but I've looked through the others and the issue doesn't seem to be the same.
Exception:
Reverse for 'booking' with arguments '()' and keyword arguments '{}' not found.
The error is thrown up with the following on the courses.html template:
{% url 'booking' %}
urls.py:
url(r'^courses/(?P<course_code>\w+)/$', views.course, name="course"),
url(r'^booking/(?P<course_code>\w+)/$', views.booking, name="booking"),
views:
def booking(request, course_code):
current_course = Course.objects.filter(short_title=course_code)
template = loader.get_template('website/booking.html')
context = Context({
'current_course': current_course,
})
return HttpResponse(template.render(context))
def courses(request):
latest_course_list = Course.objects.order_by('-start_date')
template = loader.get_template('website/courses.html')
context = Context({
'latest_course_list': latest_course_list,
})
return HttpResponse(template.render(context))
The other questions on here seem to be issues with putting quotes around the variable in the template, but this doesn't look like the same issue. Can anyone help?
Your booking url takes one parameter as course_code, hence you are getting error.
You should update the line {% url 'booking' %} appropriately to pass some course_code to the url.
So you need to update it with something like {% url 'booking' course_code %}, here I assume to have course_code parameter available in template, you can change it according to your code.
As per your url
url(r'^booking/(?P<course_code>\w+)/$', views.booking, name="booking"),
you need to pass a parameter for course_code in your template like this
{% url 'booking' course_code_value %}

Can't reverse match url in Admin Changeform

I read lot's of docs, tried everything and still can't understand why my template returns Reverse for '' with arguments '(1,)' and keyword arguments '{}' not found error. Please see error dump here: http://dpaste.com/721187/
The tag I use in change_form.html template is {% url pdfview 1 %}
The class FVatAdmin(admin.ModelAdmin) has get_urls method which looks like this:
def get_urls(self):
urls = super(FVatAdmin, self).get_urls()
my_urls = patterns('',
url(r'^view/(?P<id>\d+)', self.admin_site.admin_view(self.pdf_view), name="pdfview"),
url(r'^js/calculate', self.admin_site.admin_view(self.calculate), name="calc"),
)
return my_urls + urls
The url and pdfview defined above work just fine, but somewhat don't resolve via {% url pdfview 1 %} in a template and via reverse('pdfview', args={1}) in a view or via shell.
I just can't understand what I'm doing wrong. I'm a newbie in Django... H E L P :)
Put url name in quotes.
{% url "admin:pdfview" 1 %}
UPDATE: this applies only for Django 1.3/1.4 if:
{% load url from future %}
is used.
Django admin urls are namespaced in order not to clash with other urls.
Try doing the following {% url admin:pdfview 1 %}
See this for details:
https://docs.djangoproject.com/en/1.4/topics/http/urls/#topics-http-reversing-url-namespaces