NoReverseMatch using reverse and url with args - python

I am trying to use the reverse function in django with no luck. I already tried to change the args and use all combinations of long, int, string, unicode-string, etc, with the same error.
Help, please? Thanks.
Error I am getting:
Exception Type: NoReverseMatch at /ajax/data-request
Exception Value: Reverse for 'views.watch' with arguments '(1, 1, 'aaa')' and keyword arguments '{}' not found. 0 pattern(s) tried: []
urls.py:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$', views.index, name='index'),
url(r'^options/', views.options, name='options'),
url(r'^recuperar-contrasena/', views.recover_password, name='recover_password'),
url(r'^ajax/login', views.login_ajax, name='login_ajax'),
url(r'^ajax/data-request', views.data_request, name='data_request'),
url(r'^peliculas-populares', views.movies_popular, name='movies_popular'),
url(r'^peliculas-todas', views.movies_all, name='movies_all'),
url(r'^watch/(?P<is_movie>\d+)-(?P<id>\d+)/(?P<name>.*)$', views.watch, name='watch')
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Reverse function:
'watch-url': reverse('views.watch', None, [(is_movie), (movie.id), (slugify(movie.title))])
{% url %} that works in my template:
{% url "watch" 1 movie.id movie.title|slugify %}

The reverse() call is incorrect, should be:
reverse('watch', None, [(is_movie), (movie.id), (slugify(movie.title))]

Related

Passing context to view from template: NoReverseMatch error

Trying to pass context from template to my view (whether ad=True or False). Here's how I've tried to do it:
urls.py
url(r'^$', home, name='bv'),
url(r'^q/', search, name='search'),
url(r'^post/', include('post.urls')),
post.urls
url(r'^$', views.post, name='post'),
url(r'^edit/(?P<id>\d+)/', views.edit, name='edit'),
url(r'^delete/(?P<id>\d+)/', views.delete, name='delete'),
template
Proceed
post.views
def post(request, ad=False):
...
the ad='True' in the template should pass onto the views and change the default ad=False to ad=True. Instead, I get this error message:
NoReverseMatch at /advertise/
Reverse for 'post' with arguments '()' and keyword arguments '{'ad': 'True'}'
not found. 1 pattern(s) tried: ['post/$']
Any idea what the problem is?
change route:
url(r'^(?P<ad>\w+)$', views.post, name='post'),
and better answer:
url(r'^(?P<ad>(True|False))$', views.post, name='post'),

How to use url tag in django?

I am using Django version 1.10.7. Currently in my html I have:
</span>
My project urls.py has
from django.conf.urls import include,url
from django.contrib import admin
from base import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^team/', include('team.urls'), name='team'),
url(r'^game/', include('game.urls'), name='game'),
url(r'^about/', include('base.urls'), name='about'),
url(r'^$', views.home, name='home'),
]
And in views.py,
from django.shortcuts import render
# Create your views here.
def about(request):
return render(request,'base/about.html',{})
def home(request):
return render(request,'base/home.html',{})
This gives the error:
NoReverseMatch at /
Reverse for 'home' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.10.7
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'home' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
How can I fix this?
Try to put your urls in this order:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.home, name='home'),
url(r'^team/', include('team.urls'), name='team'),
url(r'^game/', include('game.urls'), name='game'),
url(r'^about/', include('base.urls'), name='about'),
]
if works, it means that there is a problem in your other included urls files.

Cannot find view : NoReverseMatch error

I am new to python and django and I am working on a project which is already build and I am just learning to understand it by adding some new views.
Application uses TemplateViews and I created a view which is just like the other views created. The Url pattern is also kind of same to the urls which are working fine. But my new page I created is not working and is giving me the error.
Working urls:
urls.py:
**
url(r'^aboutxyz/$', TemplateView.as_view(template_name="aboutxyz.html"), name='about_xyz'),
url(r'^contactus/$', TemplateView.as_view(template_name="contactus.html"), name='contact_us'),
**
base.html:
**
<li>About XYZ</li>
<li>Contact Us</li>
**
These 2 pages are working fine. But when I add this:
urls.py:
url(r'^aboutxyzsg/$', TemplateView.as_view(template_name="aboutxyzsg.html"), name='about_xyz_sg'),
base.html:
"<li>About XYZ sg</li>"
Here is the code from urls.py file which contains url_patterns:
urlpatterns = patterns('',
url(r'^grappelli/', include('grappelli.urls')), # grappelli URLS
url(r'^admin/', include(admin.site.urls)),
)
urlpatterns += i18n_patterns(
'',
# zinnia-tinymce
url(r'^tinymce/', include('tinymce.urls')),
url(r'^tinymce/zinnia/', include('zinnia_tinymce.urls')),
# index (home)
url(r'^$', Index.as_view(), name="main_page"),
# all-auth account
url(r'^accounts/', include('allauth.urls')),
# custom user
url(r'^accounts/', include('users.urls')),
# Privacy Policy
url(r'^privacypolicy/$', TemplateView.as_view(template_name="privacypolicy.html"), name='privacy_policy'),
# About XYZ
url(r'^contactus/$', TemplateView.as_view(template_name="contactus.html"), name='contact_us'),
url(r'^aboutxyz/$', TemplateView.as_view(template_name="aboutxyz.html"), name='about_xyz'),
url(r'^aboutxyzsg/$', TemplateView.as_view(template_name="aboutxyzsg.html"), name='about_xyz_sg'),
)
It is not working and giving me error:
My error log shows:
"NoReverseMatch: Reverse for 'about_xyz_sg' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []"
Please help me I am in deep trouble
P.S. - I tried putting aboutxyzsg url pattern before aboutxyz and still same error

Django - NoReverseMatch error in a simple form

i am stuck with this error:
"Reverse for 'create' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'$create$']"
I can't see where the problem is.
The log is an app in my general project
Here is my Project/log/templates/log/index.html that contain my form:
<form action="{% url 'log:create' %}" method="post">
//...
</form>
Here is the Project/Project/urls/py:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^$', include('log.urls', namespace='log')),
url(r'^log/', include('log.urls', namespace='log')),
url(r'^admin/', include(admin.site.urls)),
]
Here is my Project/log/urls.py :
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^login$', views.login, name='login'),
url(r'^logout$', views.logout, name='logout'),
url(r'^create$', views.create, name='create'),
]
Then I defined my function create in the Project/log/views.py :
def create(request):
if request.method == "POST" and request.POST['name'] and request.POST['password']:
name_p = escape(request.POST['name'])
pass_p = escape(request.POST['password'])
try:
login = User.objects.get(username=name_p)
except:
user = User.objects.create_user(name_p, name_p+'#email.com', pass_p)
user.save()
return HttpResponseRedirect('log:index')
else:
return render(request, 'log/index.html', {'error_message' : 'The login you chose already exists',})
else:
return render(request, 'log/index.html', {'error_message' : 'You did\'t fill the entire form',})
I don't know where is my mistake because the form is a simple one with no arguments passed and the regular expression in the url is a simple one too. It must be a problem in my function I think. If someone could lead me to the right path to fix this kind of error that would be great ;)

named url reverse does not work if url case sensitive

im trying to get my url named, so it can be reversed in my django app.
mysite/urls.py:
urlpatterns = patterns('',
# Examples:
url(r'^$', 'myapp.views.redirect_to_home'),
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include('myapp.urls')),)
myapp/urls.py
urlpatterns = patterns('',
url(r'(?i)^$', views.redirect_to_home),
url(r'(?i)^Login/$', views.home, name="home"),
url(r'(?i)^Logout/$', views.logout_user, name="logout_user"),)
be minded that i had purpusly added the '(?i)^' in the beggining of every regex, to make the urls in-case sensitive.
but, having this string '(?i)^' had made my reverse function to fail saying
Reverse for 'home' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['myapp/(?i)^Login/$']
after trying to remove the (?i)^ , i noticed the reverse succeeds.
What can i do to make my urls in-case sensitive, and still reversable?
Thanks.

Categories

Resources