Related cards:
apache django setup
ctos-7
files
file | description
/etc/httpd/conf/httpd.conf | main config file
/etc/httpd/conf.d/ | config subdir
/etc/httpd/conf.modules.d/00-base.conf | enable wsgi_module
[Instructions](https://www.digitalocean.com/community/tutori...
django apps in subfolder
sample manage.py
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
# Add the apps directoriy to Python's path. In production it will
# be n...
django transaction.set_autocommit(False)
example from: stackoverflow
from django.db import models
from django.db import transaction
class MyModel(models.Model):...
django query managers
managers
# First, define the Manager subclass.
class DahlBookManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(author='Roald Dahl')
# Then hook it into the Book model explicitly.
class Book(mod...
django primary key
Primary Key
from django.db import models
class Fruit(models.Model):
name = models.CharField(max_length=100, primary_key=True)
django foreign key self
Example
class CategoryModel(models.Model):
parentId = models.ForeignKey('self')
References
- django 2.1
- [Stack Overflow](https://stackoverflow.com/questio...
django management commands
Management Commands
Add a management/commands directory to the application. Django will register a manage.py command for each Python module in that directory whose name doesn’t begin with an underscore.
Example
from django.core.manageme...
django rename app
Before renaming
Always migrate databases before changing the app name.
manage.py makemigrations
when you see the following message then it is safe to proceed with renaming the app.
No changes detected
Fixed table names
...
django url (?P<>) regex - named capture group and backreference
Nearly all modern regular expression engines support numbered capturing groups and numbered backreferences. Long regular expressions with lots of groups and backreferences may be hard to read. They can be particularly difficult to maintain as addi...
django admin reverse url lookup
Add form
Given app is called <app_name> and model in the app is called <model_table> then the following gets the link to the "add" form on the admin page:
url = reverse('admin:<app_name>_<model_table>_add')