django template form shows the result in another html file - python

I want my search result to be displayed in another html file. I have my template as follows:
<html>
<head></head>
<body>
<div id="div1">
{% for i in var1 %}
{{i.name}}<br>
{% endfor %}
</div>
<form action="searchresult.html" method="post">{% csrf_token %}
<input type="text" name="search1">
<input type="submit" id="click1" value="search">
</form>
</body>
</html>
views.py
def search(request):
var = ""
if request.method=='POST':
s1 = request.POST.get("search1")
var = form1.objects.all().filter(keyskills=s1)
return render(request,"search.html",{'var1':var})
models.py
class form1(models.Model):
name = models.CharField(max_length=20)
qualification = models.CharField(max_length=20)
keyskills = models.CharField(max_length=50)
def __unicode__(self):
return self.name,self.qualification,self.keyskills
My result shows search details in the same page. instead I want it to be displayed in searchresult.html. I have written the searchresult.html in action of form tag. Please give me your suggestion.

Change the action attribute of your form to the URL that is mapped to the search method.
In your search method; return the searchresult.html template: return render(request, 'searchresult.html', {'var1': var1 }).
In the searchresult.html template, add:
{% for i in var1 %}
{{i.name}}<br>
{% endfor %}

It looks like form Action repesents the Html file, rather than django method to be invoked.
What you do fundamentally is this:
1) You have a form with POST operation, and while submitting, the method that you mentioned in the 'action' tag will get invoked.
2) Once you process with POST datas, then redirect to searchresult.html.
Hope this helps.

Change your action to
<form action="{% url your_project_name.your_app_name.views.search %}" method="post">
It would help if you posted your urls.py content.

Related

Implementing Search Form with Django

Hello Stackoverflow community,
I am having trouble with my form not rendering in Django.
Here's my attempt to render an empty form in views.py.
class SearchSite(forms.Form):
query = forms.CharField(label="New Item",
help_text="Search for any article located on the site.")
def search(request):
form = SearchSite()
context = {
"form": form,
"query_matches": query_matches
}
response = render(request, "encyclopedia/layout.html", context)
return response
Here's what my urls.py file looks like:
urlpatterns = [
path("", views.index, name="index"),
path("wiki/<str:page_title>", views.page, name="wiki"),
path("wiki/", views.search, name="site_search")
]
My layout.html file:
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<link href="{% static 'encyclopedia/styles.css' %}" rel="stylesheet">
</head>
<body>
<div class="row">
<div class="sidebar col-lg-2 col-md-3">
<h2>Wiki</h2>
<form action="{% url 'site_search' %}" method="get">
{% csrf_token %}
There should be something here
{{ form }}
<input type="submit">
</form>
<div>
Home
</div>
<div>
Create New Page
</div>
<div>
Random Page
</div>
{% block nav %}
{% endblock %}
</div>
<div class="main col-lg-10 col-md-9">
{% block body %}
{% endblock %}
</div>
</div>
</body>
</html>
I have noticed two particular problems in above screenshot. Firstly, my form does not render when inside my index.html webpage, which extends layout.html. Secondly, when I click the submit button, I get routed to a webpage that has my CSRF token in the url ... and then finally renders my form.
How can I fix this? Thanks everyone.
I have noticed two particular problems in above screenshot. Firstly,
my form does not render when inside my index.html webpage, which
extends layout.html.
Yes. You aren't passing form to index.html. Pass that in the view which renders the homepage. Even if it extends from layout.html, you need to pass it in the context for it to work.
def index(request):
# Your code.
return render(request, 'index.html', {'form': SearchSite()})
Secondly, when I click the submit button, I get routed to a webpage
that has my CSRF token in the url ... and then finally renders my
form.
That's because, in index.html, there is a blank form with a csrf_token, with an action set to /wiki, which calls search when the submit button is pressed. And search gives you layout.html, with the form, and as the form method is GET, it shows it in the url. I suggest changing it to POST if there is confidential data (and even otherwise. Why is there a csrf_token if it is not a POST request? Not needed. If you really want a GET request, then remove the csrf_token).
Here's my solution to the problem I had earlier for any future people visiting the post.
I wrote a form called SearchSite and defined a view called search in my views.py.
class SearchSite(forms.Form):
query = forms.CharField(
help_text="Search for any article located on the site.")
def search(request):
form = SearchSite()
is_substring_of_queries = []
if request.method == "GET":
form = SearchSite(request.GET)
if form.is_valid():
for entry in util.list_entries():
existsIdenticalResult = form.cleaned_data["query"].casefold() == entry.casefold()
existsResult = form.cleaned_data["query"].casefold() in entry.casefold()
if existsIdenticalResult:
return HttpResponseRedirect(reverse("wiki",
kwargs={"page_title": entry}))
elif existsResult:
is_substring_of_queries.append(entry)
context = {
"form": SearchSite(),
"is_substring_of_queries": is_substring_of_queries
}
response = render(request, "encyclopedia/search.html", context)
return response
When my view.search is requested, it will send the response of either an empty form (if accessed by index.html or if there are no results with a message saying there are no results) , an empty form and all the queries that are substrings of the markdown entries or route the client to an exact entry if the query matched.
Here's the routing down in my urls.py so far:
urlpatterns = [
path("", views.index, name="index"),
path("wiki/<str:page_title>", views.page, name="wiki"),
path("search/", views.search, name="site_search")
]
In my layout.html, I have the following form:
<form action="{% url 'site_search' %}" method="get">
{{ form }}
<input type="submit">
</form>
as well as in my search.html the queries that are substrings of the markdown entries:
{% if is_substring_of_queries %}
<h1>Search Results</h1>
{% for query in is_substring_of_queries%}
<li> {{ query }} </li>
{% endfor %}
{% else %}
<h1>No Results! Try again.</h1>
{% endif %}
If there are any mistakes, please let me know.

add sections/cluster from users end in html via choices or any other approach

Suppose im saving a url from a texbox given by user the URL is submitted by user and its saved to my models and now what i wanna do is let the user add different sections/clusters such as technology,sports etc from his end suppose where he inputs the URL there itself he can define that cluster/section and save that particular URL to the particular section/cluster he made or selected in the option is the section/cluster was already there! could anyone help or give me a small example for the same? what would be a better approach do it with django choices or some other way?
TIA.
ill paste the current code below :
form.py
class UrlSaveForm(ModelForm):
class Meta:
model = UrlSaveModel
fields = ['the_url']
models.py (below)
class UrlSaveModel(models.Model):
the_url = EmbedVideoField()
desc = models.CharField(max_length=200)
def __str__(self):
return self.desc
HTML for saving: save.html
{% extends 'base.html' %} {% block content %}
<form method="post" action="{% url 'func' %}">
{% csrf_token %}
<div class="form-group">
<label for="exampleFormControlTextarea1"
>Enter the URL here to save video to the library:</label
>
<textarea
class="form-control"
id="exampleFormControlTextarea1"
rows="2"
name="the_url"
placeholder="Enter the URL here"
></textarea>
<br />
<button type="submit" class="btn btn-primary mb-2">Save URL</button>
</div>
</form>
{% endblock %}
"""and little snippet of views.py"""
if form.is_valid():
print('form is valid')
posted_url = request.POST['the_url']
yt = YouTube(posted_url)
description = yt.title
print('description-extracted:', description)
print('Posted_url', posted_url)
obj = UrlSaveModel(desc=description, the_url=posted_url)
obj.save()
return HttpResponse('saved sucessfully!')

Passing variable with POST in django

Hi im trying to pass a name from a form to a view in django using POST. There are no errors in the execution but its passing nothing from the template and dont know if i doing something wrong here. Im starting with django so i can have newbie errors. If u need more information tell me pls.
Views.py
def crear_pdf(request):
empresa_selec = ""
form = EmpModelForm()
if request.method == 'POST':
form = EmpModelForm(data=request.POST)
if form.is_valid():
empresa_selec = form.cleaned_data['nombre']
#"empresa_selec" that's the empty variable
Models.py
class Empresa_modelo(models.Model):
nombre = models.CharField(max_length=100,blank=True,null=True)
Forms.py
class EmpModelForm(forms.ModelForm):
class Meta:
model = Empresa_modelo
fields = ["nombre"]
template.html
<div class="container-fluid">
<form method="POST" enctype="multipart/form-data" action="{% url 'crear_pdf' %}">{% csrf_token %}
<p>Empresa</p>
<input type="text" name="empresa">
<br>
<button type="submit">Subir</button>
</form>
<br>
<a class="btn btn-primary" href="{% url 'crear_pdf' %}">Atras</a>
</div>
You haven't got a field called nombre in your template; you only have empresa.
That's presumably because you don't ouput your EmpModelForm in the template. You don't show your render call in the view, but assuming you pass it as form, you should just do {{ form.as_p }} in the template.
Try using:
<input type="text" name="nombre">
There is no field named empresa.
Had a look at your code,there are a couple of issues.First you are not using the model form defined in your forms.py file in your template. Second you have defined an input text box with the name that you are not referring in your views. Either use the model form or use the same name of your input text box in your views.
def crear_pdf(request):
empresa_selec = ""
form = EmpModelForm()
if request.method == 'POST':
form = EmpModelForm(data=request.POST)
if form.is_valid():
empresa_selec = form.cleaned_data['nombre']
else:
return render(request,"template.html",{"form":form})
And in your template you can edit as such:
<div class="container-fluid">
<form method="POST" enctype="multipart/form-data" action="{% url 'crear_pdf' %}">{% csrf_token %}
{{ form.as_p }}
<br>
<button type="submit">Subir</button>
</form>
<br>
<a class="btn btn-primary" href="{% url 'crear_pdf' %}">Atras</a>
</div>
Hope this helps.

Display form input with Django

So basically I want to make a simple form I can enter text and the after I hit submit, see the text.
Here is my forms.py:
class Search(forms.Form):
search = forms.CharField()
Here is my views.py:
def search(request):
context = RequestContext(request)
if request.method == 'POST':
search = Search(data=request.POST)
if search.is_valid():
ticker = search.save()
ticker.save()
success = True
else:
print search.errors
else:
search = Search()
return render_to_response('ui/search.html', {"search":search}, context)
Here is the html form that you use to type in (I'm using bootstrap for styling purposes):
<form class="navbar-form navbar-right" role="search" action="/search/" method="post" name="tick">
{% csrf_token %}
<div class="form-group">
<input type="text" class="form-control" placeholder="Enter stock symbol">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
And finally, I want the text entered in the form to be displayed on "search.html" which looks like this currently:
{% extends 'ui/base.html' %}
{% block title %} search {% endblock %}
{% block body_block %}
<br>
<p>test</p>
{{ form.search.data }} <!--I'm pretty sure this is not correct -->
{% endblock %}
Anyone know how I can do this? Thanks.
Your form name is search.
To render the value with modern django, you need to call the value method of the field, therefore your template should look like the following:
{{ search.search.value }}
Your template is wrong, as you suspect.
It is looking for a context variable named "form", but you have given it a context dictionary with a key named "search".
Also, "data" is the argument that you use to build up your Search object (correctly), but when you want to extract the user's input from it, you should use the field names instead, and you need to call value() on them in order to get the bound value. So, to get the contents of the text field called search, you should use search.search.value.
Try changing the line
{{ form.search.data }}
to
{{ search.search.value }}

Django form errors are not showing up

I have looked all over stackoverflow and the Internet about this so I will just show my code.
views.py
def UserSell(request,username):
theuser=User.objects.get(username=username)
thegigform=GigForm()
#if the user is submitting a form
if request.method=='POST':
#bind form with form inputs and image
gigform=GigForm(request.POST,request.FILES)
if gigform.is_valid():
gigform.title=gigform.cleaned_data['title']
gigform.description=gigform.cleaned_data['description']
gigform.more_info=gigform.cleaned_data['more_info']
gigform.time_for_completion=gigform.cleaned_data['time_for_completion']
gigform.gig_image=gigform.cleaned_data['gig_image']
finalgigform=gigform.save(commit=False)
finalgigform.from_user=theuser
finalgigform.save()
return HttpResponseRedirect('done')
thegigform=GigForm()
context=RequestContext(request)
return render_to_response('sell.html',{'theuser':theuser,'thegigform':thegigform},context_instance=context)
template
<form action="{% url sell user.username %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<fieldset>
<legend><h2>Sell A Gig</h2></legend>
{% for f in thegigform %}
<div class="formWrapper">
{{f.errors}}
{{f.label_tag}}: {{f}}
{{f.help_text}}
</div>
{% endfor %}
</fieldset>
<input type="submit" value="Sell Now!" />
This code seems to follow normal django form protocol so please tell me why my django template doesnt show the errors. Thanks
It looks like you are missing an else block.
if gigform.valid() returns false, you are overwriting the variable "thegigform". Try restructuring your code like this:
if request.method=='POST':
#bind form with form inputs and image
thegigform=GigForm(request.POST,request.FILES)
if thegigform.is_valid():
thegigform.title=gigform.cleaned_data['title']
thegigform.description=gigform.cleaned_data['description']
thegigform.more_info=gigform.cleaned_data['more_info']
thegigform.time_for_completion=gigform.cleaned_data['time_for_completion']
thegigform.gig_image=gigform.cleaned_data['gig_image']
finalgigform=gigform.save(commit=False)
finalgigform.from_user=theuser
finalgigform.save()
return HttpResponseRedirect('done')
else:
thegigform=GigForm()
context=RequestContext(request)
return render_to_response('sell.html',{'theuser':theuser,'thegigform':thegigform},context_instance=context)

Categories