django apps in subfolder
Given a sub-directory named "apps" for all django applications, the following steps are used to setup the sub-directory and create apps.
1 create app sub folder - "apps"
mkdir apps
touch apps/__init__.py
2 add app to sub folder:
mkdir apps/myapp
python manage.py startapp myapp apps/myapp
3 update apps.py in apps/myapp to have the name include 'apps.' as shown below:
class MyappConfig(AppConfig):
# optional, add default auto field
default_auto_field = 'django.db.models.BigAutoField'
# set location of ...
pycharm sources root
To make a source visible go into the settings and select project -> project structure then select the directory that contains the sources root.
https://www.jetbrains.com/help/pycharm/configuring-content-roots.html
pycharm modules
Use the module option for the run configuration, see under "scrip path:module name"
See Also
drf jwt
https://simpleisbetterthancomplex.com/tutorial/2018/12/19/how-to-use-jwt-authentication-with-django-rest-framework.html
django multiple databases
Step 1: Setup DB in settings
Step 2: Setup DB Routing
Step 3: Migration
Step 4: Testing
Reference
django CSRF Disable
Sample class for CSRF disable
from rest_framework.authentication import SessionAuthentication, BasicAuthentication
class CsrfExemptSessionAuthentication(SessionAuthentication):
def enforce_csrf(self, request):
return # To not perform the csrf check previously happening
In view add
class MyTableDetailApiView(generics.RetrieveUpdateDestroyAPIView):
queryset = MyTable.objects.all()
serializer_class = MyTableSerializer
authentication_classes = (CsrfExemptSessionAuthentication, BasicAuthentication)
vue delimiters in django
Sample delimiter replacement for django
<script>
new Vue({
delimiters: ['[[', ']]'],
el: '#app',
data: {
title: 'Welcome to My Journal'
}
})
</script>
See Also
pycharm django project root
In setttings go
language & Frameworks
Django
Set djano project root
set default timeout
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')
Apache gunicorn
See Also
- django gunicorn install
- How To Setup
- Detailed Stetup
- SSL
- apache ProxyPass
- [virtualenv gunicorn setup](https://www.digitaloc...
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
For this case it is assumed the table names are fixed in models.py
Rename and Loose migration
- remove migrations from db:
manage.py migrate --fake old_app_name zero
- remove migration files from app
find old_app_name -path "*/migrations/*.py" \
-not -name "__init__.py" \
...
apache django mod_wsgi
install mod_wsgi.so
add to base.conf
nano /etc/httpd/conf.modules.d/00-base.conf
add this line
LoadModule wsgi_module modules/mod_wsgi.so
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
django create app in subdir
To create a "polls" app in the "apps" sub-directory, do the following first to make the directory (assuming you are at the root of your django project):
mkdir apps/polls
Next, run startapp to create "polls" in under the "app" project directory.
startapp polls apps/polls
Install the app;
INSTALLED_APPS = [
'apps.polls'
]
migrate:
makemigrations polls
migrate
optional:
main urls.py
urlpatterns = [
path('polls/',include('apps.polls.urls'),
]
See Also
django url parameters and view functions
url.py
url(r'^stores/',stores_views.detail,{'location':'headquarters'})
views.py
def detail(request,location=None):
Reference
django migrate
basic
makemigration <app>
migrate
with python path
if an apps dir is used to organize the apps then do the following
makemigrations myapp --pythonpath apps
migrate
See also
django foreign key self
Example
class CategoryModel(models.Model):
parentId = models.ForeignKey('self')
References
django admin action
Sample duplicate event
This example makes use of clearing the id field (primary key) and then saves the record to produce a duplicate of the record. The user can select as many records from the admin panel and it will duplicate each in the selected query set.
# admin.py
from django.contrib import admin
import django.contrib.admin.options as admin_opt
def dup_event(modeladmin:admin_opt.ModelAdmin, request, queryset):
for object in queryset:
from_id = object.id
object.id = None
object.save()
messa...