0

I'm doing a project of CS50's Intro to Web Programming with Javascript and Python(Pset1 : Wiki). While creating a new entry/page I'm asking the user for a title and content for the page. After which I'm appending(adding) the title in the url to which the user should be taken. But the problem is that if the user enters some whitespace in the title then anything after the whitespace is not included in the url. Kindly suggest me if there is anyway to include the whitespace in the url.

User's input:

User entering the title

Generated link in html:

Generated link in html

After the user enters the title and content and clicks submit, the entry is saved here:

Entries

Code which creates the url from user input:

    <ul>
    {% for entry in entries %}
        <li> <a href=/wiki/{{entry}}>{{ entry }}</a></li>

    {% endfor %}
    </ul>

Views.py

from django.shortcuts import render
from markdown2 import Markdown
from . import util
from django import forms
import random

markdowner = Markdown()


class NewPage(forms.Form):
    title = forms.CharField()
    content = forms.CharField(widget=forms.Textarea)


def index(request):
    return render(request, "encyclopedia/index.html", {
        "entries": util.list_entries()
    })


def title(request, title):
    entry = util.list_entries()
    if title in entry:
        page = util.get_entry(title)
        page_convert = markdowner.convert(page)
        context = {"title": title, "page_display": page_convert}

        return render(request, "encyclopedia/user_entry.html", context)
    else:
        return render(request, "encyclopedia/error.html",
                      {"message": "Sorry! The page that you searched was not found."})


def new_page(request):
    if request.method == "POST":
        form = NewPage(request.POST)
        if form.is_valid():
            title = form.cleaned_data["title"]
            content = "#" + title + "\n" + form.cleaned_data["content"]
            if title not in util.list_entries():
                util.save_entry(title, content)
                page = util.get_entry(title)
                page_converted = markdowner.convert(page)
                return render(request, "encyclopedia/user_entry.html", {"title": title, "page_display": page_converted})
            else:
                return render(request, "encyclopedia/error.html",
                              {"message": "A wikipedia entry already exists by this name."})

    else:
        return render(request, "encyclopedia/newpage.html", {"form": NewPage, "title": "New Page"})


def random_page(request):
    if request.method == "GET":
        entry = util.list_entries()
        entry_count = len(util.list_entries())
        random_no = random.randrange(0, entry_count - 1)
        randompage = entry[random_no]
        page = util.get_entry(randompage)
        page_convert = markdowner.convert(page)
        context = {"title": randompage, "page_display": page_convert}
        return render(request, "encyclopedia/user_entry.html", context)

2
  • Could you please add your views.py file?
    – Vsjain
    Commented Apr 4, 2021 at 14:23
  • Ok sure I'm adding views.py as well. Commented Apr 4, 2021 at 14:24

1 Answer 1

1

You can try replacing whitespaces with a hyphen before saving the entry. By doing this, you save the complete name of the entry and can use it as a url. For this, the search and title rendering will have to modified a bit. While saving the entry use title = title.replace(' ', '-'). A similar approach can be used for searching. For displaying in html, you can take some help from https://stackoverflow.com/a/21483185

Hope this helps. If it does then please check the tickmark.

3
  • Well thanks for the idea but i have no clue how to make some modifications in the title entered by the user in django or html. I searched on the web and there was some function that can be used in javascript to encode the url. Is there any simillar function in html?Can you brief how to implement this idea(title rendering)? Commented Apr 5, 2021 at 7:38
  • I have edited the answer to include the modifications
    – Vsjain
    Commented Apr 5, 2021 at 9:09
  • Thanks a lot for your help. It worked Commented Apr 5, 2021 at 9:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .