Python Forum
Help with new Django Project
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with new Django Project
#4
OK that makes sense. I'm trying some simple code to see if it works. For some reason it's not printing the search results for me.

Layout.html:

{% 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 class="row">
            <div class="sidebar col-lg-2 col-md-3">
                <h2>Wiki</h2>
                <form>
                    <input class="search" type="text" name="search" placeholder="Search Encyclopedia">
                </form>
                <div>
                    <a href="{% url 'index' %}">Home</a>
                </div>
                <div>
                    Create New Page
                </div>
                <div>
                    Random Page 
                </div>
                {% block nav %}
                {% endblock %}
            </div>
            <div class="main col-lg-10 col-md-9">
                {% block body %}
                {% endblock %}
            </div>
        </div>

    </body>
</html>
views.py:

from django.shortcuts import render

from . import util


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

def wiki(request, title):
    return render(request, "encyclopedia/wiki.html", {
        "entries": util.get_entry(title)
    })

def search(request, search):
    search = request.form.get("search")
    print(search)
    return render(request, "encyclopedia/search.html", {
        "entries": util.get_entry(search)
    })
    

here's my util.py file:

import re

from django.core.files.base import ContentFile
from django.core.files.storage import default_storage


def list_entries():
    """
    Returns a list of all names of encyclopedia entries.
    """
    _, filenames = default_storage.listdir("entries")
    return list(sorted(re.sub(r"\.md$", "", filename)
                for filename in filenames if filename.endswith(".md")))


def save_entry(title, content):
    """
    Saves an encyclopedia entry, given its title and Markdown
    content. If an existing entry with the same title already exists,
    it is replaced.
    """
    filename = f"entries/{title}.md"
    if default_storage.exists(filename):
        default_storage.delete(filename)
    default_storage.save(filename, ContentFile(content))


def get_entry(title):
    """
    Retrieves an encyclopedia entry by its title. If no such
    entry exists, the function returns None.
    """
    try:
        f = default_storage.open(f"entries/{title}.md")
        return f.read().decode("utf-8")
    except FileNotFoundError:
        return None
Reply


Messages In This Thread
Help with new Django Project - by card51shor - Jul-02-2020, 09:31 PM
RE: Help with new Django Project - by card51shor - Jul-03-2020, 04:36 AM
RE: Help with new Django Project - by ndc85430 - Jul-03-2020, 05:00 AM
RE: Help with new Django Project - by card51shor - Jul-03-2020, 05:09 AM
RE: Help with new Django Project - by ndc85430 - Jul-03-2020, 05:36 AM
RE: Help with new Django Project - by card51shor - Jul-03-2020, 05:40 AM
RE: Help with new Django Project - by ndc85430 - Jul-03-2020, 05:48 AM
RE: Help with new Django Project - by card51shor - Jul-03-2020, 05:52 AM
RE: Help with new Django Project - by ndc85430 - Jul-03-2020, 06:27 AM
RE: Help with new Django Project - by card51shor - Jul-03-2020, 07:04 AM
RE: Help with new Django Project - by ndc85430 - Jul-04-2020, 07:26 AM
RE: Help with new Django Project - by card51shor - Jul-03-2020, 06:15 PM
RE: Help with new Django Project - by card51shor - Jul-04-2020, 07:44 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Hello I’m trying to run a Django python project on docker but when I try to build the Nessa404 1 1,755 Apr-22-2022, 12:25 PM
Last Post: VadimCr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020