Passing context to view from template: NoReverseMatch error - python

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'),

Related

How can I refer to other urls' captured values in Django?

So, I am writing code with Python and Django (and other web dev requirements).
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path('wiki/<str:name>/', views.info, name="info"),
path("create", views.create, name="create"),
path("results", views.search, name="search"),
path("wiki/<str:name>/edit", views.edit, name="edit")
]
The problem I am facing is that in path("wiki/<str:name>/edit", views.edit, name="edit"), I get an error:
NoReverseMatch at /wiki/Django/>
Reverse for 'edit' with no arguments not found. 1 pattern(s) tried: ['wiki/(?P[^/]+)/edit$']>
And I want to refer to the url in path('wiki/<str:name>/', views.info, name="info")
Please help me fix the error

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.

Django NoReverseMatch at accounts/password_reset/

This seems like a common problem people are running into, but I can't figure it out for the life of me. I've set up a URL:
from django.conf.urls import url
from . import views
from django.contrib.auth import views as auth_views
urlpatterns = [
url(r'^login/$', views.login_view, name='login'),
url(r'^password_reset/$', auth_views.password_reset, name='password_reset'),
url(r'^password_reset_done/$', auth_views.password_reset_done, name='password_reset_done'),
url(r'^auth/$', views.login_auth, name='login_auth'),
url(r'^register/$', views.register_user, name='register'),
url(r'^logout/$', views.logout_user, name='logout'),
]
I try to link to the password_reset URL:
Forgot Password?
But it gives me the following error:
NoReverseMatch at /accounts/password_reset/
Reverse for 'password_reset_done' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
This seems like a really simple bit of code, but it doesn't work. I can successfully call password_reset_done using this exact method, but for some reason it doesn't work with password_reset.
The password_reset line should look like this:
url(r'^password_reset/$', auth_views.password_reset, {'post_reset_redirect' : '/accounts/password_reset_done/'}, name='password_reset'),

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 ;)

NoReverseMatch using reverse and url with args

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))]

Categories

Resources