my layout:
{% 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 >
<div >
<h2>Wiki</h2>
<form action = "{% url 'search' %}" method="GET">
<input type="text" name="q" placeholder="Search Encyclopedia">
</form>
<div>
<a href="{% url 'index' %}">Home</a>
</div>
<div>
<a href = "{% url 'newpage' %}">Create New Page</a>
</div>
<div>
<a>Random Page </a>
</div>
{% block nav %}
{% endblock %}
</div>
<div >
{% block body %}
{% endblock %}
</div>
</div>
</body>
</html>
my views.py:
def search(request):
result = set()
entries = util.list_entries()
if request.method == 'GET':
query = request.GET.get('q')
if query == '': #If it's nothing
query = 'NONE'
if query in entries:
return redirect(f'wiki/{query}')
else:
results = [entry for entry in entries if query.lower() in entry.lower()]
return render(request, "encyclopedia/index.html", {
"entries": results
})
return render(request, 'encyclopedia/search.html', {'results':result})
my urls:
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from . import views
app_name = 'encyclopedia'
urlpatterns = [
path("", views.index, name="index"),
path('newpage/', views.new_page, name = 'newpage'),
path('wiki/<str:title>', views.entry, name = 'entries'),
path('wiki/<str:title>/edit',views.edit, name = 'edit'),
path('search', views.search, name = 'search'),
path('wiki/<str:entry>', views.random_page, name = 'random'),
]
urlpatterns = static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
How do i make it work? it worked before but now it just stopped working. The name in the urls matches with the name of a bar What i have to know about url method? I also tried to write {% url 'random'%} for a random page but it seems not working at all When i added this to a random search url stopped working and i do not know why
Versions: Django 4.1.4, Python 3.10.5
CodePudding user response:
You have declared app_name in your urls.py file that makes it compulsory to add app_name before calling a url in django. Call your urls like this...
href={% url 'app_name:url_name' %}
So your code will be like this...
{% 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 >
<div >
<h2>Wiki</h2>
<form action = "{% url 'encyclopedia:search' %}" method="GET">
<input type="text" name="q" placeholder="Search Encyclopedia">
</form>
<div>
<a href="{% url 'encyclopedia:index' %}">Home</a>
</div>
<div>
<a href = "{% url 'encyclopedia:newpage' %}">Create New Page</a>
</div>
<div>
<a>Random Page </a>
</div>
{% block nav %}
{% endblock %}
</div>
<div >
{% block body %}
{% endblock %}
</div>
</div>
</body>
</html>