Redirecting user values from one template to another - Django
up vote
1
down vote
favorite
Trying to redirect the values given by the user from one page to the next.
Everything can be done in one view, but when I try to redirect to the next one using HttpResponseRedirect Django return error 'NameError at /search_results, name '' is not defined'. How to pass the 'text' value from one view to another (to my search results)
My views.py (Works well, the values given by the user in one field, return the corresponding results of the cure from django-filters)
def test_views(request):
form = MeanForm(request.POST)
if form.is_valid():
text = form.cleaned_data['name']
else:
text = None
search_users = SearchWoman(request.GET, queryset=Woman.objects.all().filter(city=text))
context = 'form': form, 'text': text, 'filter': search_users
return render(request, 'test.html', context)
My test.html
<h1>TEST_1</h1>
<form method="POST" class="post-form">
% csrf_token %
form.as_p
<button type="submit" class="save btn btn-default">Submit</button>
</form>
<h2> text </h2>
<h1><br></br></h1>
% for profile in filter.qs %
<li> profile.name </li>
% endfor %
My filters.py
from .models import Woman
import django_filters
class SearchWoman(django_filters.FilterSet):
class Meta:
model = Woman
fields = ['city', 'rating']
My forms.py
from django import forms
from .models import Mean
class MeanForm(forms.ModelForm):
class Meta:
model = Mean
fields = ('name',)
How I try to do a redirect (it returns error "NameError at / search_results, nazwa" "is not defined")
def test_views(request):
form = MeanForm(request.POST)
if form.is_valid():
text = form.cleaned_data['name']
return HttpResponseRedirect('/search_results/')
else:
text = None
context = 'form': form, 'text': text,
return render(request, 'test.html', context)
def search_results(request):
search_users = SearchWoman(request.GET, queryset=Woman.objects.all().filter(city=text))
context = 'search_user': search_users
return render(request, 'search_results.html', context)
Tempaltes Error (after applying the second view)
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/search_results/
Django Version: 2.1.3
Python Version: 3.7.0
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'host_app',
'django_filters']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangocorehandlersexception.py" in inner
34. response = get_response(request)
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangocorehandlersbase.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangocorehandlersbase.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:UserstymotDesktopagencja_modeli_modelekapp_ramahost_appviews.py" in search_results
59. search_users = SearchWoman(request.GET, queryset=Woman.objects.all().filter(city=text))
Exception Type: NameError at /search_results/
Exception Value: name 'text' is not defined
**EDIT: error code: **
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/test/
Django Version: 2.1.3
Python Version: 3.7.0
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'host_app',
'django_filters']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangocorehandlersexception.py" in inner
34. response = get_response(request)
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangocorehandlersbase.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangocorehandlersbase.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:UserstymotDesktopagencja_modeli_modelekapp_ramahost_appviews.py" in test_views
50. return HttpResponseRedirect(reverse('search_results', args=[text]))
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangourlsbase.py" in reverse
90. return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangourlsresolvers.py" in _reverse_with_prefix
622. raise NoReverseMatch(msg)
Exception Type: NoReverseMatch at /test/
Exception Value: Reverse for 'search_results' not found. 'search_results' is not a valid view function or pattern name.
URLS App
from django.conf.urls import url
from .import views
app_name = 'host_app'
urlpatterns = [
[...]
url(r'^test/$', views.test_views, name='test_views'),
url(r'^search_results/(?P<text>[w-]+)/$', views.search_results, name='search_results')
]
URLS Rama (next to settings.py)
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('host_app.urls', namespace='host_app')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
python django django-filter
add a comment |
up vote
1
down vote
favorite
Trying to redirect the values given by the user from one page to the next.
Everything can be done in one view, but when I try to redirect to the next one using HttpResponseRedirect Django return error 'NameError at /search_results, name '' is not defined'. How to pass the 'text' value from one view to another (to my search results)
My views.py (Works well, the values given by the user in one field, return the corresponding results of the cure from django-filters)
def test_views(request):
form = MeanForm(request.POST)
if form.is_valid():
text = form.cleaned_data['name']
else:
text = None
search_users = SearchWoman(request.GET, queryset=Woman.objects.all().filter(city=text))
context = 'form': form, 'text': text, 'filter': search_users
return render(request, 'test.html', context)
My test.html
<h1>TEST_1</h1>
<form method="POST" class="post-form">
% csrf_token %
form.as_p
<button type="submit" class="save btn btn-default">Submit</button>
</form>
<h2> text </h2>
<h1><br></br></h1>
% for profile in filter.qs %
<li> profile.name </li>
% endfor %
My filters.py
from .models import Woman
import django_filters
class SearchWoman(django_filters.FilterSet):
class Meta:
model = Woman
fields = ['city', 'rating']
My forms.py
from django import forms
from .models import Mean
class MeanForm(forms.ModelForm):
class Meta:
model = Mean
fields = ('name',)
How I try to do a redirect (it returns error "NameError at / search_results, nazwa" "is not defined")
def test_views(request):
form = MeanForm(request.POST)
if form.is_valid():
text = form.cleaned_data['name']
return HttpResponseRedirect('/search_results/')
else:
text = None
context = 'form': form, 'text': text,
return render(request, 'test.html', context)
def search_results(request):
search_users = SearchWoman(request.GET, queryset=Woman.objects.all().filter(city=text))
context = 'search_user': search_users
return render(request, 'search_results.html', context)
Tempaltes Error (after applying the second view)
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/search_results/
Django Version: 2.1.3
Python Version: 3.7.0
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'host_app',
'django_filters']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangocorehandlersexception.py" in inner
34. response = get_response(request)
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangocorehandlersbase.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangocorehandlersbase.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:UserstymotDesktopagencja_modeli_modelekapp_ramahost_appviews.py" in search_results
59. search_users = SearchWoman(request.GET, queryset=Woman.objects.all().filter(city=text))
Exception Type: NameError at /search_results/
Exception Value: name 'text' is not defined
**EDIT: error code: **
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/test/
Django Version: 2.1.3
Python Version: 3.7.0
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'host_app',
'django_filters']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangocorehandlersexception.py" in inner
34. response = get_response(request)
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangocorehandlersbase.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangocorehandlersbase.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:UserstymotDesktopagencja_modeli_modelekapp_ramahost_appviews.py" in test_views
50. return HttpResponseRedirect(reverse('search_results', args=[text]))
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangourlsbase.py" in reverse
90. return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangourlsresolvers.py" in _reverse_with_prefix
622. raise NoReverseMatch(msg)
Exception Type: NoReverseMatch at /test/
Exception Value: Reverse for 'search_results' not found. 'search_results' is not a valid view function or pattern name.
URLS App
from django.conf.urls import url
from .import views
app_name = 'host_app'
urlpatterns = [
[...]
url(r'^test/$', views.test_views, name='test_views'),
url(r'^search_results/(?P<text>[w-]+)/$', views.search_results, name='search_results')
]
URLS Rama (next to settings.py)
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('host_app.urls', namespace='host_app')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
python django django-filter
Please include the full traceback for the error. Where are you usingnazwa
? Do you meantext
? You can pass that either by including it in the url as a query parameterhttps://example.com/search_results/?text=foobar
or by stashing the value in the session object. django docs: How to use sessionsrequest.session['text'] = 'foobar'
– Håken Lid
Nov 10 at 14:51
Yes, I meant the text, I'm sorry for the mistake. The full description of the error is now added to the main comment
– Maddie Graham
Nov 10 at 18:39
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Trying to redirect the values given by the user from one page to the next.
Everything can be done in one view, but when I try to redirect to the next one using HttpResponseRedirect Django return error 'NameError at /search_results, name '' is not defined'. How to pass the 'text' value from one view to another (to my search results)
My views.py (Works well, the values given by the user in one field, return the corresponding results of the cure from django-filters)
def test_views(request):
form = MeanForm(request.POST)
if form.is_valid():
text = form.cleaned_data['name']
else:
text = None
search_users = SearchWoman(request.GET, queryset=Woman.objects.all().filter(city=text))
context = 'form': form, 'text': text, 'filter': search_users
return render(request, 'test.html', context)
My test.html
<h1>TEST_1</h1>
<form method="POST" class="post-form">
% csrf_token %
form.as_p
<button type="submit" class="save btn btn-default">Submit</button>
</form>
<h2> text </h2>
<h1><br></br></h1>
% for profile in filter.qs %
<li> profile.name </li>
% endfor %
My filters.py
from .models import Woman
import django_filters
class SearchWoman(django_filters.FilterSet):
class Meta:
model = Woman
fields = ['city', 'rating']
My forms.py
from django import forms
from .models import Mean
class MeanForm(forms.ModelForm):
class Meta:
model = Mean
fields = ('name',)
How I try to do a redirect (it returns error "NameError at / search_results, nazwa" "is not defined")
def test_views(request):
form = MeanForm(request.POST)
if form.is_valid():
text = form.cleaned_data['name']
return HttpResponseRedirect('/search_results/')
else:
text = None
context = 'form': form, 'text': text,
return render(request, 'test.html', context)
def search_results(request):
search_users = SearchWoman(request.GET, queryset=Woman.objects.all().filter(city=text))
context = 'search_user': search_users
return render(request, 'search_results.html', context)
Tempaltes Error (after applying the second view)
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/search_results/
Django Version: 2.1.3
Python Version: 3.7.0
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'host_app',
'django_filters']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangocorehandlersexception.py" in inner
34. response = get_response(request)
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangocorehandlersbase.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangocorehandlersbase.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:UserstymotDesktopagencja_modeli_modelekapp_ramahost_appviews.py" in search_results
59. search_users = SearchWoman(request.GET, queryset=Woman.objects.all().filter(city=text))
Exception Type: NameError at /search_results/
Exception Value: name 'text' is not defined
**EDIT: error code: **
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/test/
Django Version: 2.1.3
Python Version: 3.7.0
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'host_app',
'django_filters']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangocorehandlersexception.py" in inner
34. response = get_response(request)
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangocorehandlersbase.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangocorehandlersbase.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:UserstymotDesktopagencja_modeli_modelekapp_ramahost_appviews.py" in test_views
50. return HttpResponseRedirect(reverse('search_results', args=[text]))
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangourlsbase.py" in reverse
90. return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangourlsresolvers.py" in _reverse_with_prefix
622. raise NoReverseMatch(msg)
Exception Type: NoReverseMatch at /test/
Exception Value: Reverse for 'search_results' not found. 'search_results' is not a valid view function or pattern name.
URLS App
from django.conf.urls import url
from .import views
app_name = 'host_app'
urlpatterns = [
[...]
url(r'^test/$', views.test_views, name='test_views'),
url(r'^search_results/(?P<text>[w-]+)/$', views.search_results, name='search_results')
]
URLS Rama (next to settings.py)
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('host_app.urls', namespace='host_app')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
python django django-filter
Trying to redirect the values given by the user from one page to the next.
Everything can be done in one view, but when I try to redirect to the next one using HttpResponseRedirect Django return error 'NameError at /search_results, name '' is not defined'. How to pass the 'text' value from one view to another (to my search results)
My views.py (Works well, the values given by the user in one field, return the corresponding results of the cure from django-filters)
def test_views(request):
form = MeanForm(request.POST)
if form.is_valid():
text = form.cleaned_data['name']
else:
text = None
search_users = SearchWoman(request.GET, queryset=Woman.objects.all().filter(city=text))
context = 'form': form, 'text': text, 'filter': search_users
return render(request, 'test.html', context)
My test.html
<h1>TEST_1</h1>
<form method="POST" class="post-form">
% csrf_token %
form.as_p
<button type="submit" class="save btn btn-default">Submit</button>
</form>
<h2> text </h2>
<h1><br></br></h1>
% for profile in filter.qs %
<li> profile.name </li>
% endfor %
My filters.py
from .models import Woman
import django_filters
class SearchWoman(django_filters.FilterSet):
class Meta:
model = Woman
fields = ['city', 'rating']
My forms.py
from django import forms
from .models import Mean
class MeanForm(forms.ModelForm):
class Meta:
model = Mean
fields = ('name',)
How I try to do a redirect (it returns error "NameError at / search_results, nazwa" "is not defined")
def test_views(request):
form = MeanForm(request.POST)
if form.is_valid():
text = form.cleaned_data['name']
return HttpResponseRedirect('/search_results/')
else:
text = None
context = 'form': form, 'text': text,
return render(request, 'test.html', context)
def search_results(request):
search_users = SearchWoman(request.GET, queryset=Woman.objects.all().filter(city=text))
context = 'search_user': search_users
return render(request, 'search_results.html', context)
Tempaltes Error (after applying the second view)
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/search_results/
Django Version: 2.1.3
Python Version: 3.7.0
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'host_app',
'django_filters']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangocorehandlersexception.py" in inner
34. response = get_response(request)
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangocorehandlersbase.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangocorehandlersbase.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:UserstymotDesktopagencja_modeli_modelekapp_ramahost_appviews.py" in search_results
59. search_users = SearchWoman(request.GET, queryset=Woman.objects.all().filter(city=text))
Exception Type: NameError at /search_results/
Exception Value: name 'text' is not defined
**EDIT: error code: **
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/test/
Django Version: 2.1.3
Python Version: 3.7.0
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'host_app',
'django_filters']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangocorehandlersexception.py" in inner
34. response = get_response(request)
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangocorehandlersbase.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangocorehandlersbase.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:UserstymotDesktopagencja_modeli_modelekapp_ramahost_appviews.py" in test_views
50. return HttpResponseRedirect(reverse('search_results', args=[text]))
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangourlsbase.py" in reverse
90. return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "C:UserstymotDesktopagencja_modeli_modelekenvlibsite-packagesdjangourlsresolvers.py" in _reverse_with_prefix
622. raise NoReverseMatch(msg)
Exception Type: NoReverseMatch at /test/
Exception Value: Reverse for 'search_results' not found. 'search_results' is not a valid view function or pattern name.
URLS App
from django.conf.urls import url
from .import views
app_name = 'host_app'
urlpatterns = [
[...]
url(r'^test/$', views.test_views, name='test_views'),
url(r'^search_results/(?P<text>[w-]+)/$', views.search_results, name='search_results')
]
URLS Rama (next to settings.py)
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('host_app.urls', namespace='host_app')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
python django django-filter
python django django-filter
edited yesterday
asked Nov 10 at 11:09
Maddie Graham
12912
12912
Please include the full traceback for the error. Where are you usingnazwa
? Do you meantext
? You can pass that either by including it in the url as a query parameterhttps://example.com/search_results/?text=foobar
or by stashing the value in the session object. django docs: How to use sessionsrequest.session['text'] = 'foobar'
– Håken Lid
Nov 10 at 14:51
Yes, I meant the text, I'm sorry for the mistake. The full description of the error is now added to the main comment
– Maddie Graham
Nov 10 at 18:39
add a comment |
Please include the full traceback for the error. Where are you usingnazwa
? Do you meantext
? You can pass that either by including it in the url as a query parameterhttps://example.com/search_results/?text=foobar
or by stashing the value in the session object. django docs: How to use sessionsrequest.session['text'] = 'foobar'
– Håken Lid
Nov 10 at 14:51
Yes, I meant the text, I'm sorry for the mistake. The full description of the error is now added to the main comment
– Maddie Graham
Nov 10 at 18:39
Please include the full traceback for the error. Where are you using
nazwa
? Do you mean text
? You can pass that either by including it in the url as a query parameter https://example.com/search_results/?text=foobar
or by stashing the value in the session object. django docs: How to use sessions request.session['text'] = 'foobar'
– Håken Lid
Nov 10 at 14:51
Please include the full traceback for the error. Where are you using
nazwa
? Do you mean text
? You can pass that either by including it in the url as a query parameter https://example.com/search_results/?text=foobar
or by stashing the value in the session object. django docs: How to use sessions request.session['text'] = 'foobar'
– Håken Lid
Nov 10 at 14:51
Yes, I meant the text, I'm sorry for the mistake. The full description of the error is now added to the main comment
– Maddie Graham
Nov 10 at 18:39
Yes, I meant the text, I'm sorry for the mistake. The full description of the error is now added to the main comment
– Maddie Graham
Nov 10 at 18:39
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
You can try like this with reverse to send parameters to next view:
# views
def test_views(request):
form = MeanForm(request.POST)
if form.is_valid():
text = form.cleaned_data['name']
eturn HttpResponseRedirect(reverse('search_result', args=[text]))
else:
text = None
context = 'form': form, 'text': text,
return render(request, 'test.html', context)
def search_results(request, text):
search_users = SearchWoman(request.GET, queryset=Woman.objects.all().filter(city=text))
context = 'search_user': search_users
return render(request, 'search_results.html', context)
# urls
path('search_result/<str:text>/', search_results, name="search_result")
# urls for django 1.11 or older versions
url(r'^search_result/(?P<text>[w-]+)/$',search_results, name="search_result")
The documentation looks interesting, thank you for your answer. But so far I am getting a mistake from the template. ` NoReverseMatch at /test/ Reverse for '/search_results/' not found. '/search_results/' is not a valid view function or pattern name.` My files after changes in the comment below. Did I write something wrong?
– Maddie Graham
Nov 10 at 18:30
In the urls, there is a parameter calledname
, resolve actually uses this to reverse find your actual url path. So, first you need to update that in the views and the urls. If still reverse throws a problem, then please post the traceback with the original question.
– ruddra
Nov 11 at 2:58
@MaddieGraham please see my updated answer with url
– ruddra
2 days ago
1
please see my answer, where I have updated the view code.
– ruddra
yesterday
1
Everything is working properly. Thank you very much for help.
– Maddie Graham
15 hours ago
|
show 5 more comments
up vote
1
down vote
Since you are using django-filters, it would make sense to put the data in the query parameters of the redirect.
from django.utils.http import urlencode
from django.urls import reverse
from django.http import HttpResponseRedirect
def test_views(request):
city, rating = 'Springfield', 11 # or get them from a form
query_string = urlencode('city': city, 'rating': rating)
next_url = '?'.format(reverse(search_results), query_string)
return HttpResonseRedirect(next_url)
def search_results(request):
search_users = SearchWoman(request.GET)
# when redirected, the url and request.GET contains data from previous view
return render(request, 'search_results.html', 'search_users': search_users)
Another way to pass data is to use the session object. This requires that django's session middleware is active and the client uses cookies. Both are standard, so it should work well for a typical web site.
from django.urls import reverse
from django.http import HttpResponseRedirect
def test_views(request):
city, rating = 'Springfield'
request.session['city'] = city # set session['city']
return HttpResonseRedirect(reverse(search_results)
def search_results(request):
city = request.session.get('city') # get session['city']
data = request.GET.dict() # get url query parameters if any
if city:
data['city'] = city
search_users = SearchWoman(data)
return render(request, 'search_results.html', 'search_users': search_users)
Sessions are easy to use. If you want to learn more about how sessions work, read this section of the django docs: How to use sessions
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
You can try like this with reverse to send parameters to next view:
# views
def test_views(request):
form = MeanForm(request.POST)
if form.is_valid():
text = form.cleaned_data['name']
eturn HttpResponseRedirect(reverse('search_result', args=[text]))
else:
text = None
context = 'form': form, 'text': text,
return render(request, 'test.html', context)
def search_results(request, text):
search_users = SearchWoman(request.GET, queryset=Woman.objects.all().filter(city=text))
context = 'search_user': search_users
return render(request, 'search_results.html', context)
# urls
path('search_result/<str:text>/', search_results, name="search_result")
# urls for django 1.11 or older versions
url(r'^search_result/(?P<text>[w-]+)/$',search_results, name="search_result")
The documentation looks interesting, thank you for your answer. But so far I am getting a mistake from the template. ` NoReverseMatch at /test/ Reverse for '/search_results/' not found. '/search_results/' is not a valid view function or pattern name.` My files after changes in the comment below. Did I write something wrong?
– Maddie Graham
Nov 10 at 18:30
In the urls, there is a parameter calledname
, resolve actually uses this to reverse find your actual url path. So, first you need to update that in the views and the urls. If still reverse throws a problem, then please post the traceback with the original question.
– ruddra
Nov 11 at 2:58
@MaddieGraham please see my updated answer with url
– ruddra
2 days ago
1
please see my answer, where I have updated the view code.
– ruddra
yesterday
1
Everything is working properly. Thank you very much for help.
– Maddie Graham
15 hours ago
|
show 5 more comments
up vote
1
down vote
accepted
You can try like this with reverse to send parameters to next view:
# views
def test_views(request):
form = MeanForm(request.POST)
if form.is_valid():
text = form.cleaned_data['name']
eturn HttpResponseRedirect(reverse('search_result', args=[text]))
else:
text = None
context = 'form': form, 'text': text,
return render(request, 'test.html', context)
def search_results(request, text):
search_users = SearchWoman(request.GET, queryset=Woman.objects.all().filter(city=text))
context = 'search_user': search_users
return render(request, 'search_results.html', context)
# urls
path('search_result/<str:text>/', search_results, name="search_result")
# urls for django 1.11 or older versions
url(r'^search_result/(?P<text>[w-]+)/$',search_results, name="search_result")
The documentation looks interesting, thank you for your answer. But so far I am getting a mistake from the template. ` NoReverseMatch at /test/ Reverse for '/search_results/' not found. '/search_results/' is not a valid view function or pattern name.` My files after changes in the comment below. Did I write something wrong?
– Maddie Graham
Nov 10 at 18:30
In the urls, there is a parameter calledname
, resolve actually uses this to reverse find your actual url path. So, first you need to update that in the views and the urls. If still reverse throws a problem, then please post the traceback with the original question.
– ruddra
Nov 11 at 2:58
@MaddieGraham please see my updated answer with url
– ruddra
2 days ago
1
please see my answer, where I have updated the view code.
– ruddra
yesterday
1
Everything is working properly. Thank you very much for help.
– Maddie Graham
15 hours ago
|
show 5 more comments
up vote
1
down vote
accepted
up vote
1
down vote
accepted
You can try like this with reverse to send parameters to next view:
# views
def test_views(request):
form = MeanForm(request.POST)
if form.is_valid():
text = form.cleaned_data['name']
eturn HttpResponseRedirect(reverse('search_result', args=[text]))
else:
text = None
context = 'form': form, 'text': text,
return render(request, 'test.html', context)
def search_results(request, text):
search_users = SearchWoman(request.GET, queryset=Woman.objects.all().filter(city=text))
context = 'search_user': search_users
return render(request, 'search_results.html', context)
# urls
path('search_result/<str:text>/', search_results, name="search_result")
# urls for django 1.11 or older versions
url(r'^search_result/(?P<text>[w-]+)/$',search_results, name="search_result")
You can try like this with reverse to send parameters to next view:
# views
def test_views(request):
form = MeanForm(request.POST)
if form.is_valid():
text = form.cleaned_data['name']
eturn HttpResponseRedirect(reverse('search_result', args=[text]))
else:
text = None
context = 'form': form, 'text': text,
return render(request, 'test.html', context)
def search_results(request, text):
search_users = SearchWoman(request.GET, queryset=Woman.objects.all().filter(city=text))
context = 'search_user': search_users
return render(request, 'search_results.html', context)
# urls
path('search_result/<str:text>/', search_results, name="search_result")
# urls for django 1.11 or older versions
url(r'^search_result/(?P<text>[w-]+)/$',search_results, name="search_result")
edited 2 days ago
answered Nov 10 at 14:43
ruddra
7,31832546
7,31832546
The documentation looks interesting, thank you for your answer. But so far I am getting a mistake from the template. ` NoReverseMatch at /test/ Reverse for '/search_results/' not found. '/search_results/' is not a valid view function or pattern name.` My files after changes in the comment below. Did I write something wrong?
– Maddie Graham
Nov 10 at 18:30
In the urls, there is a parameter calledname
, resolve actually uses this to reverse find your actual url path. So, first you need to update that in the views and the urls. If still reverse throws a problem, then please post the traceback with the original question.
– ruddra
Nov 11 at 2:58
@MaddieGraham please see my updated answer with url
– ruddra
2 days ago
1
please see my answer, where I have updated the view code.
– ruddra
yesterday
1
Everything is working properly. Thank you very much for help.
– Maddie Graham
15 hours ago
|
show 5 more comments
The documentation looks interesting, thank you for your answer. But so far I am getting a mistake from the template. ` NoReverseMatch at /test/ Reverse for '/search_results/' not found. '/search_results/' is not a valid view function or pattern name.` My files after changes in the comment below. Did I write something wrong?
– Maddie Graham
Nov 10 at 18:30
In the urls, there is a parameter calledname
, resolve actually uses this to reverse find your actual url path. So, first you need to update that in the views and the urls. If still reverse throws a problem, then please post the traceback with the original question.
– ruddra
Nov 11 at 2:58
@MaddieGraham please see my updated answer with url
– ruddra
2 days ago
1
please see my answer, where I have updated the view code.
– ruddra
yesterday
1
Everything is working properly. Thank you very much for help.
– Maddie Graham
15 hours ago
The documentation looks interesting, thank you for your answer. But so far I am getting a mistake from the template. ` NoReverseMatch at /test/ Reverse for '/search_results/' not found. '/search_results/' is not a valid view function or pattern name.` My files after changes in the comment below. Did I write something wrong?
– Maddie Graham
Nov 10 at 18:30
The documentation looks interesting, thank you for your answer. But so far I am getting a mistake from the template. ` NoReverseMatch at /test/ Reverse for '/search_results/' not found. '/search_results/' is not a valid view function or pattern name.` My files after changes in the comment below. Did I write something wrong?
– Maddie Graham
Nov 10 at 18:30
In the urls, there is a parameter called
name
, resolve actually uses this to reverse find your actual url path. So, first you need to update that in the views and the urls. If still reverse throws a problem, then please post the traceback with the original question.– ruddra
Nov 11 at 2:58
In the urls, there is a parameter called
name
, resolve actually uses this to reverse find your actual url path. So, first you need to update that in the views and the urls. If still reverse throws a problem, then please post the traceback with the original question.– ruddra
Nov 11 at 2:58
@MaddieGraham please see my updated answer with url
– ruddra
2 days ago
@MaddieGraham please see my updated answer with url
– ruddra
2 days ago
1
1
please see my answer, where I have updated the view code.
– ruddra
yesterday
please see my answer, where I have updated the view code.
– ruddra
yesterday
1
1
Everything is working properly. Thank you very much for help.
– Maddie Graham
15 hours ago
Everything is working properly. Thank you very much for help.
– Maddie Graham
15 hours ago
|
show 5 more comments
up vote
1
down vote
Since you are using django-filters, it would make sense to put the data in the query parameters of the redirect.
from django.utils.http import urlencode
from django.urls import reverse
from django.http import HttpResponseRedirect
def test_views(request):
city, rating = 'Springfield', 11 # or get them from a form
query_string = urlencode('city': city, 'rating': rating)
next_url = '?'.format(reverse(search_results), query_string)
return HttpResonseRedirect(next_url)
def search_results(request):
search_users = SearchWoman(request.GET)
# when redirected, the url and request.GET contains data from previous view
return render(request, 'search_results.html', 'search_users': search_users)
Another way to pass data is to use the session object. This requires that django's session middleware is active and the client uses cookies. Both are standard, so it should work well for a typical web site.
from django.urls import reverse
from django.http import HttpResponseRedirect
def test_views(request):
city, rating = 'Springfield'
request.session['city'] = city # set session['city']
return HttpResonseRedirect(reverse(search_results)
def search_results(request):
city = request.session.get('city') # get session['city']
data = request.GET.dict() # get url query parameters if any
if city:
data['city'] = city
search_users = SearchWoman(data)
return render(request, 'search_results.html', 'search_users': search_users)
Sessions are easy to use. If you want to learn more about how sessions work, read this section of the django docs: How to use sessions
add a comment |
up vote
1
down vote
Since you are using django-filters, it would make sense to put the data in the query parameters of the redirect.
from django.utils.http import urlencode
from django.urls import reverse
from django.http import HttpResponseRedirect
def test_views(request):
city, rating = 'Springfield', 11 # or get them from a form
query_string = urlencode('city': city, 'rating': rating)
next_url = '?'.format(reverse(search_results), query_string)
return HttpResonseRedirect(next_url)
def search_results(request):
search_users = SearchWoman(request.GET)
# when redirected, the url and request.GET contains data from previous view
return render(request, 'search_results.html', 'search_users': search_users)
Another way to pass data is to use the session object. This requires that django's session middleware is active and the client uses cookies. Both are standard, so it should work well for a typical web site.
from django.urls import reverse
from django.http import HttpResponseRedirect
def test_views(request):
city, rating = 'Springfield'
request.session['city'] = city # set session['city']
return HttpResonseRedirect(reverse(search_results)
def search_results(request):
city = request.session.get('city') # get session['city']
data = request.GET.dict() # get url query parameters if any
if city:
data['city'] = city
search_users = SearchWoman(data)
return render(request, 'search_results.html', 'search_users': search_users)
Sessions are easy to use. If you want to learn more about how sessions work, read this section of the django docs: How to use sessions
add a comment |
up vote
1
down vote
up vote
1
down vote
Since you are using django-filters, it would make sense to put the data in the query parameters of the redirect.
from django.utils.http import urlencode
from django.urls import reverse
from django.http import HttpResponseRedirect
def test_views(request):
city, rating = 'Springfield', 11 # or get them from a form
query_string = urlencode('city': city, 'rating': rating)
next_url = '?'.format(reverse(search_results), query_string)
return HttpResonseRedirect(next_url)
def search_results(request):
search_users = SearchWoman(request.GET)
# when redirected, the url and request.GET contains data from previous view
return render(request, 'search_results.html', 'search_users': search_users)
Another way to pass data is to use the session object. This requires that django's session middleware is active and the client uses cookies. Both are standard, so it should work well for a typical web site.
from django.urls import reverse
from django.http import HttpResponseRedirect
def test_views(request):
city, rating = 'Springfield'
request.session['city'] = city # set session['city']
return HttpResonseRedirect(reverse(search_results)
def search_results(request):
city = request.session.get('city') # get session['city']
data = request.GET.dict() # get url query parameters if any
if city:
data['city'] = city
search_users = SearchWoman(data)
return render(request, 'search_results.html', 'search_users': search_users)
Sessions are easy to use. If you want to learn more about how sessions work, read this section of the django docs: How to use sessions
Since you are using django-filters, it would make sense to put the data in the query parameters of the redirect.
from django.utils.http import urlencode
from django.urls import reverse
from django.http import HttpResponseRedirect
def test_views(request):
city, rating = 'Springfield', 11 # or get them from a form
query_string = urlencode('city': city, 'rating': rating)
next_url = '?'.format(reverse(search_results), query_string)
return HttpResonseRedirect(next_url)
def search_results(request):
search_users = SearchWoman(request.GET)
# when redirected, the url and request.GET contains data from previous view
return render(request, 'search_results.html', 'search_users': search_users)
Another way to pass data is to use the session object. This requires that django's session middleware is active and the client uses cookies. Both are standard, so it should work well for a typical web site.
from django.urls import reverse
from django.http import HttpResponseRedirect
def test_views(request):
city, rating = 'Springfield'
request.session['city'] = city # set session['city']
return HttpResonseRedirect(reverse(search_results)
def search_results(request):
city = request.session.get('city') # get session['city']
data = request.GET.dict() # get url query parameters if any
if city:
data['city'] = city
search_users = SearchWoman(data)
return render(request, 'search_results.html', 'search_users': search_users)
Sessions are easy to use. If you want to learn more about how sessions work, read this section of the django docs: How to use sessions
answered Nov 10 at 19:51
Håken Lid
10.3k62440
10.3k62440
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238340%2fredirecting-user-values-from-one-template-to-another-django%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Please include the full traceback for the error. Where are you using
nazwa
? Do you meantext
? You can pass that either by including it in the url as a query parameterhttps://example.com/search_results/?text=foobar
or by stashing the value in the session object. django docs: How to use sessionsrequest.session['text'] = 'foobar'
– Håken Lid
Nov 10 at 14:51
Yes, I meant the text, I'm sorry for the mistake. The full description of the error is now added to the main comment
– Maddie Graham
Nov 10 at 18:39