Skip to main content
added 2259 characters in body
Source Link

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)

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)

Source Link

Is there a way to replace the whitespace in the url whose part is some user input? i.e. User input contains whitespaces Pset1 (Wiki)

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>