Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 [^]

HTML
{% 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 [^]
Python
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"), #we were expecting q to come but putting action in form will provide a suitable path which is
                                                 #which is search form  action = "search"
    path("newpage/", views.newpage, name="newpage"), 
]

views.py image [^]
Python
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'] #getting the value of q from the form as it's name is 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":
        #taking title input
        title = request.POST['title']
        #taking content input
        content=request.POST['content']
        #checking if title exist
        entry = util.get_entry(title)
        #if checking if entry is not None
        if entry is None:
            #saving entry
            util.save_entry(title, content)
            #converting entry for redirect to that page
            html_content=convert_md_to_html(title)
            #opening currently saved entry
            return render(request, "encyclopedia/css.html", {
                 "content": html_content
            })
        else: 
            #rendering alert message if title doesn't exists
            return render(request, "encyclopedia/newpage.html", {
                "message": "This Tittle already exists!"
            })
    else:
            #rendering page on get request
            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.
Posted
Updated 11-Sep-23 10:51am
v3
Comments
Richard MacCutchan 3-Sep-23 12:29pm    
Please post the code inside your question (with proper <pre> tags). Most people here will not bother to follow offsite links to find the information.
Sachin Sep2023 3-Sep-23 22:41pm    
Ok sir
Richard MacCutchan 4-Sep-23 3:46am    
I have just had a look at the Cs50web page, and it may be a better idea to ask for help there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900