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

drf jwt

https://simpleisbetterthancomplex.com/tutorial/2018/12/19/how-to-use-jwt-authentication-with-django-rest-framework.html

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

vuejs with django

django admin user

python manage.py createsuperuser

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 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

  1. remove migrations from db:
manage.py migrate --fake old_app_name zero
  1. 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

Instructions

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 migrate

django url parameters and view functions

url.py

url(r'^stores/',stores_views.detail,{'location':'headquarters'})

views.py

def detail(request,location=None):

Reference

url parameters

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 apps in subfolder

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...