I am doing Cs50web course and working on a project using Django. I have created a path in
urls.py of application and it has two requests,
Get
and
Post
.
It's a form page where you can add the
Title
,
Content
and a button to save:
On encyclopedia/newpage as get request page gets open and on http://<ip>/encyclopedia/newpage url as a post request page get saved. However, When page is open in the browser as http://127.0.0.1:80000/encyclopedia/newpage by clicking the
Save button, the url becomes http://127.0.0.1:80000/encyclopedia/newpage/newpage and shows an error.
newpage.html image [
^]
{% extends "encyclopedia/layout.html" %}
{% block title %}
Create New Page
{% endblock %}
{% block body %}
<h1>Create New Page</h1>
<form action="newpage/" method="post">
{% csrf_token %}
<input type="text" name="title" id="title_box"
placeholder="Enter title">
<br>
<br>
<textarea placeholder="Enter Your Content here"
name="content"></textarea>
<br>
<button type="submit" name="save"
class="btn btn-primary">Save</button>
</form>
{% if message %}
<script>
alert("{{ message }}");
</script>
{% endif %}
{% endblock %}
urls.py image [
^]
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("wiki/<str:name>", views.wiki, name="wiki"),
path("search/", views.search, name="search"),
path("newpage/", views.newpage, name="newpage"),
]
views.py image [
^]
from django.shortcuts import render
from markdown import Markdown
from . import util
from . import views
def index(request):
return render(request, "encyclopedia/index.html", {
"entries": util.list_entries()
})
def convert_md_to_html(title):
entries = util.get_entry(title)
markdowner = Markdown()
if entries == None:
return None
else:
return markdowner.convert(entries)
def wiki(request, name):
check = views.convert_md_to_html(name)
if check == None:
return render(request, "encyclopedia/error.html")
else:
return render(request, "encyclopedia/css.html", {
"content": check
})
def search(request):
if request.method == "POST":
entry = request.POST['q']
html_content = convert_md_to_html(entry)
if html_content is not None:
return render(request, "encyclopedia/css.html", {
"content": html_content
})
else:
all_entries = util.list_entries()
recommendation = []
for entries in all_entries:
if entry.lower() in entries.lower():
recommendation.append(entries)
return render(request, "encyclopedia/search.html", {
"recommendation": recommendation
})
def newpage(request):
if request.method == "POST":
title = request.POST['title']
content=request.POST['content']
entry = util.get_entry(title)
if entry is None:
util.save_entry(title, content)
html_content=convert_md_to_html(title)
return render(request, "encyclopedia/css.html", {
"content": html_content
})
else:
return render(request, "encyclopedia/newpage.html", {
"message": "This Tittle already exists!"
})
else:
return render(request, "encyclopedia/newpage.html")
What I have tried:
I am new to programing and especially Django, so I don't know what to do. Please help! I appreciate any help from you.