I am new to django and I have I think a pretty fundamental question.
Lets say I have this theme:
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.
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 .
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.
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”.
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.
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 %}
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With