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
2 days ago
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
2 days ago
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 12 hours ago
asked 2 days ago
Maddie Graham
12711
12711
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
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
2 days ago
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
2 days ago
@MaddieGraham please see my updated answer with url
– ruddra
yesterday
I try to implement your solution, but the template still returns the same error (after entering the value in the search engine). Full description of the error above.
– Maddie Graham
13 hours ago
@MaddieGraham can you add your url structures please? (also please includeurls.py
which resides besidesettings.py
)
– ruddra
13 hours ago
|
show 4 more comments
up vote
0
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
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
2 days ago
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
2 days ago
@MaddieGraham please see my updated answer with url
– ruddra
yesterday
I try to implement your solution, but the template still returns the same error (after entering the value in the search engine). Full description of the error above.
– Maddie Graham
13 hours ago
@MaddieGraham can you add your url structures please? (also please includeurls.py
which resides besidesettings.py
)
– ruddra
13 hours ago
|
show 4 more comments
up vote
1
down vote
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
2 days ago
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
2 days ago
@MaddieGraham please see my updated answer with url
– ruddra
yesterday
I try to implement your solution, but the template still returns the same error (after entering the value in the search engine). Full description of the error above.
– Maddie Graham
13 hours ago
@MaddieGraham can you add your url structures please? (also please includeurls.py
which resides besidesettings.py
)
– ruddra
13 hours ago
|
show 4 more comments
up vote
1
down vote
up vote
1
down vote
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 yesterday
answered 2 days ago
ruddra
7,29332546
7,29332546
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
2 days ago
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
2 days ago
@MaddieGraham please see my updated answer with url
– ruddra
yesterday
I try to implement your solution, but the template still returns the same error (after entering the value in the search engine). Full description of the error above.
– Maddie Graham
13 hours ago
@MaddieGraham can you add your url structures please? (also please includeurls.py
which resides besidesettings.py
)
– ruddra
13 hours ago
|
show 4 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
2 days ago
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
2 days ago
@MaddieGraham please see my updated answer with url
– ruddra
yesterday
I try to implement your solution, but the template still returns the same error (after entering the value in the search engine). Full description of the error above.
– Maddie Graham
13 hours ago
@MaddieGraham can you add your url structures please? (also please includeurls.py
which resides besidesettings.py
)
– ruddra
13 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
2 days 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
2 days ago
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
2 days ago
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
2 days ago
@MaddieGraham please see my updated answer with url
– ruddra
yesterday
@MaddieGraham please see my updated answer with url
– ruddra
yesterday
I try to implement your solution, but the template still returns the same error (after entering the value in the search engine). Full description of the error above.
– Maddie Graham
13 hours ago
I try to implement your solution, but the template still returns the same error (after entering the value in the search engine). Full description of the error above.
– Maddie Graham
13 hours ago
@MaddieGraham can you add your url structures please? (also please include
urls.py
which resides beside settings.py
)– ruddra
13 hours ago
@MaddieGraham can you add your url structures please? (also please include
urls.py
which resides beside settings.py
)– ruddra
13 hours ago
|
show 4 more comments
up vote
0
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
0
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
0
down vote
up vote
0
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 2 days ago
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
2 days ago
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
2 days ago