Sitemap
David Buchs

David Buchs

Using Django to create the static page

2 min readSep 28, 2023

I realized that for my purposes, I don’t have to deploy my Django app for it to be useful — it’s overkill for this project.

But I can practice creating a local Django app that will use a template to generate the static file that I do want to host. I could even build in some tool for publishing the new file once it has been updated. This tool, besides pushing the new file to its destination, will also have to make some changes — e.g., the location of static files like the images and styles.

python manage.py startapp <app_name>

That’s the command to give me my apps.py, models.py, and views.py.

Get David Buchs’s stories in your inbox

Join Medium for free to get updates from this writer.

A new model in models.py:

from django.db import models

# Create your models here.
class Faq(models.Model):
question_text = models.CharField(max_length=200)
answer_text = models.TextField()

def __str__(self):
return self.question_text + ":\n" + self.answer_text

Register that model in admin.py:

from django.contrib import admin
from .models import Faq

# Register your models here.
admin.site.register(Faq)

Make all the migrations, and then use BeautifulSoup to parse my current static html and pull the questions and answers:

>>> page = requests.get(URL)
>>> soup = BeautifulSoup(page.content, "html.parser")
>>> results = soup.find(id='faqContainer')
>>> articles = results.find_all("article")
>>> for article in articles:
... q = Faq(question_text = article.find("button").text, answer_text = article.find("p").text)
... q.save()

Success!

--

--