Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store static text on a website with django

I am new to django and I have I think a pretty fundamental question.

Lets say I have this theme:

enter image description here

I made a project already, so I know a bit about know how to build models for dynamic content, pass them to views and admin panel etc, but:

Question: on the image above I marked 3 containers that include text. There is only one instance of this text on the whole website, and it's not repeatable. If I developed for myself I would just hard-code that, but what if I develop for a client, who needs to be able to edit those fields using the admin panel?

Am I supposed to create a separate class containing multiple (lets say 20) fields for these kind of containers for the whole website, pass that class in a view (and filter with [:1]) to use it in a template?

Thats the only thing I came up with. Although it would work I think it's a terrible solution.

like image 709
Chris Avatar asked Sep 24 '12 00:09

Chris


People also ask

How do I serve static files in Django?

Serving the site and your static files from the same serverPush your code up to the deployment server. On the server, run collectstatic to copy all the static files into STATIC_ROOT . Configure your web server to serve the files in STATIC_ROOT under the URL STATIC_URL .

Where are Django static files stored?

Files are searched by using the enabled finders . The default is to look in all locations defined in STATICFILES_DIRS and in the 'static' directory of apps specified by the INSTALLED_APPS setting.

What is static file in Django?

Aside from the HTML generated by the server, web applications generally need to serve additional files — such as images, JavaScript, or CSS — necessary to render the complete web page. In Django, we refer to these files as “static files”.

What is static root in Django?

The STATIC_ROOT variable in settings.py defines the single folder you want to collect all your static files into. Typically, this would be a top-level folder inside your project, eg: STATIC_ROOT = "/home/myusername/myproject/static" # or, eg, STATIC_ROOT = os. path.


2 Answers

What I would do is write a model that contains a TextField for the blurb to insert and a CharField to identify it, and a custom template tag that reads the blurb from the database by the argument you pass to it.

class Blurb(models.Model):
  ident = models.CharField(..., db_index=True)
  blurb = models.TextField(...)

PK  ident  text
1   main   Hey! Do you like this template? This...

{% load blurb %}
 ...
{% blurb main %}
like image 101
Ignacio Vazquez-Abrams Avatar answered Sep 30 '22 13:09

Ignacio Vazquez-Abrams


you could have 1 model with a selection field containing a descriptor for the text in the model.

Something like:

class SomeText(models.Model):
    position = models.CharField(max_length=120, choices=POSITION_DESCRIPTORS)
    text = models.TextField()
like image 20
monkut Avatar answered Sep 30 '22 14:09

monkut