[英]What part of the website from Django/ Python to change to get a pre-written text in search bar?
I have the .css which hard codes the place of the search bar. 我有.css,它对搜索栏的位置进行了硬编码。 The search bar is using a variable, keywords, called in the forms.py.
搜索栏正在使用在forms.py中调用的变量关键字。 Lastly the view.py might be another possibility to get the pre-populated text of "Search for a tool" in the search bar.
最后,view.py可能是在搜索栏中获取“搜索工具”的预填充文本的另一种可能性。
Which of these codes can I set the value= "Search for a tool", like I do in the HTML editor in the browser? 像在浏览器的HTML编辑器中一样,可以设置以下哪个代码为“ Search for a tool”?
style.css File probably is the most confusing for me because I can change the initial definitions to [type = "text", value = "Search for a tool"], and then the search bar moves to a new location on the html page and does not have the pre=written text. style.css文件可能对我来说最令人困惑,因为我可以将初始定义更改为[type =“ text”,value =“ Search for a tool”],然后将搜索栏移动到html页面上的新位置并且没有预先写好的文字。
...
.search{
width:350px;
margin-left:120px;
float:left;
position:relative;
height:75px;
}
.search input[type="text"], .search input[type='number'], .search input[type="password"] {
margin:20px;
margin-top:25px;
padding: 4px; background:#d8f6fd;
float:right;
border: 1px solid #66c8de;
-moz-box-shadow: 0 1px 1px #ddd inset, 0 1px 0 #fff;
-webkit-box-shadow: 0 1px 1px #ddd inset, 0 1px 0 #fff;
box-shadow: 0 1px 1px #ddd inset, 0 1px 0 #fff;
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
border-radius: 3px;
}
.search input[type="submit"]{
margin-top:25px;
margin-right:25px;
float:right;
}
...
The forms.py . form.py。 This uses a char field.
这使用一个char字段。 I get many errors when I try to add parameters in CharField( blank = False, default = "Enter a tool name") I am guessing there is nothing that would initialize the search bar with this .py
当我尝试在CharField中添加参数时出现很多错误(blank = False,默认=“输入工具名称”)我猜测没有任何东西可以用此.py初始化搜索栏
class Search(forms.Form):
keywords = forms.CharField()
THe views.py Seems like it will only process a request from the search bar, and I would assume that it could initialize. views.py似乎它只会处理来自搜索栏的请求,因此我认为它可以初始化。
def search(request):
template = loader.get_template('sharesystem/search.html')
form = Search()
if request.method == 'GET':
form = Search(request.GET)
if form.is_valid():
keywords = form.cleaned_data['keywords']
tools = Tool.objects.filter(name__contains = keywords).values()
if (keywords is ''):
tools = Tool.objects.all()
context = RequestContext(request, {
'tools' : tools, 'form':form })
return HttpResponse(template.render(context))
tools = Tool.objects.all()
context = RequestContext(request, {
'form' : form,'tools' : tools})
return HttpResponse(template.render(context))
To follow up base.html When i add a new line for input, I get mutiple search boxes. 跟踪base.html添加新行作为输入时,会出现多个搜索框。 When class='search' is called, what do I put in that div?
当class ='search'被调用时,该放在那个div中呢?
<div id="headerwrapper">
<div class="centerframe">
<div id="logo">
<a href="{% url 'sharesystem:index' %}" ><img src="{{STATIC_URL}}sharesystem/images/logo.png"border="none" ></a>
</div>
<div class='search' >
<input type="text" name="keywords" placeholder="Enter a tool name"/>
<form action="/sharesystem/search/" method="GET" style='margin-bottom:0;'>
{% csrf_token %}
<input type="submit" value="Search" />
{{ search_form.keywords }}
</form>
</div>
<div style="padding-top:24px;">
<form action="/sharesystem/search/" method="GET" style='margin-bottom:0;'>
{% csrf_token %}
<input type="submit" value="View All Tools" />
</form>
</div>
</div>
</div>
Depending on your business needs, and what browsers you need to support, I'd like to recommend using the HTML input 'placeholder' attribute. 根据您的业务需求以及需要支持的浏览器,我建议您使用HTML输入的“占位符”属性。
You would need to either override your forms init function and update the widget attrs dictionary from there or explicitly set the widget in your CharField declaration. 您可能需要覆盖表单init函数并从那里更新窗口小部件attrs字典,或者在CharField声明中显式设置窗口小部件。
keywords = forms.CharField( widget=forms.TextInput(attrs={'placeholder': 'Search for a tool'})) 关键字= form.CharField(widget = forms.TextInput(attrs = {'占位符':'搜索工具'}))
使用input
HTML标记的placeholder
属性:
<input type="text" name="keywords" placeholder="Enter a tool name">
You should take a loo into initial data for Forms. 您应该对Forms的初始数据有所了解。
form = Form(initial={'value': 'My Search!'})
In your case: form = Search() if request.method == 'GET': form = Search(request.GET, initial={'value': 'My Search!'}) 在您的情况下:form = Search()if request.method =='GET':form = Search(request.GET,initial = {'value':'My Search!'})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.