In the sidebar of my homepage, there is a search box where a user is meant to be able to type in the name of an entry and be taken to that page. So for example, if they type in css
they would be taken to http://127.0.0.1:8000/wiki/css
where the value inputted is appended to the url. (The home page url is http://127.0.0.1:8000/wiki/
.)
When I use the get
method in the form it ends up searching up /wiki/?q=css
instead of just having /css
at the end of the url. However, when I use the post
method in my form the url remains at http://127.0.0.1:8000/wiki/
with no additional url at the end.
How do I append an input to the end of a url?
HTML:
<h2>Wiki</h2>
<form action="/wiki/" method="post">
{% csrf_token %}
<input type="text" name="q" placeholder="Search Encyclopedia">
</form>
urls.py:
from tokenize import Name
from unicodedata import name
from django.urls import path
from django.http import HttpResponse
from . import views
urlpatterns = [
path("", views.index, name="index"),
path('hello/', views.hello, name='hello'),
path('<str:name>', views.entry, name='entry'),
path('new/', views.new, name='new'),
path('random/', views.randomEntry, name='random')
]
views.py:
from django.shortcuts import render
from django.http import HttpResponse
from django import forms
import random
from . import util
def index(request):
return render(request, "encyclopedia/index.html", {
"entries": util.list_entries()
})
def hello(request):
return HttpResponse('Hello, world')
def entry(request, name):
return HttpResponse(util.get_entry(name))
def search(name):
value = util.get_entry(name)
if value != None:
return value
class searchForm(forms.Form):
search = forms.CharField(label='Search Encyclopedia')
# this was not used as I do not know if it would be useful
def new(request):
return render(request, "encyclopedia/new.html", {
"entries": util.list_entries()
})
def randomEntry(request):
list = []
for i in util.list_entries():
list.append(i)
num = random.randint(0, len(list)-1)
page = list[num]
return render(request, f"entries/layout.html", {
"entries": util.list_entries()
})
util.py (I don't know if this is useful but it could be used):
import re
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage
def list_entries():
"""
Returns a list of all names of encyclopedia entries.
"""
_, filenames = default_storage.listdir("entries")
return list(sorted(re.sub(r"\.md$", "", filename)
for filename in filenames if filename.endswith(".md")))
def save_entry(title, content):
"""
Saves an encyclopedia entry, given its title and Markdown
content. If an existing entry with the same title already exists,
it is replaced.
"""
filename = f"entries/{title}.md"
if default_storage.exists(filename):
default_storage.delete(filename)
default_storage.save(filename, ContentFile(content))
def get_entry(title):
"""
Retrieves an encyclopedia entry by its title. If no such
entry exists, the function returns None.
"""
try:
f = default_storage.open(f"entries/{title}.md")
return f.read().decode("utf-8")
except FileNotFoundError:
return None
CodePudding user response:
Have your input form post to a new view that retrieves the field value and then redirects to a your wiki entry based on that value
html
<h2>Wiki</h2>
<form action="{% url 'wiki-lookup' %}" method="post">
views.py
def wiki-lookup(request):
#first we get the posted q term, or return 'notfound' if the form fails to provide a value
term = request.POST.get('q', 'notfound')
#....test term for validity here as appropriate
#next redirect to 'entry' passing the term as the name value for that URL
return redirect('entry', name = term)
urls.py
...
path('wiki-lookup/', views.wiki-lookup, name='wiki-lookup'),
Django is actually quite neat in that views don't have to be actual pages - they can just do some work to determine which page the user should actually end up at.