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 ...
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
Use the module option for the run configuration, see under "scrip path:module name"
https://simpleisbetterthancomplex.com/tutorial/2018/12/19/how-to-use-jwt-authentication-with-django-rest-framework.html
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)
Sample delimiter replacement for django
<script>
new Vue({
delimiters: ['[[', ']]'],
el: '#app',
data: {
title: 'Welcome to My Journal'
}
})
</script>
In setttings go
language & Frameworks
Django
Set djano project root
set default timeout
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')
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
For this case it is assumed the table names are fixed in models.py
manage.py migrate --fake old_app_name zero
find old_app_name -path "*/migrations/*.py" \
-not -name "__init__.py" \
...
nano /etc/httpd/conf.modules.d/00-base.conf
add this line
LoadModule wsgi_module modules/mod_wsgi.so
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
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'),
]
url.py
url(r'^stores/',stores_views.detail,{'location':'headquarters'})
views.py
def detail(request,location=None):
makemigration <app>
migrate
if an apps dir is used to organize the apps then do the following
makemigrations myapp --pythonpath apps
migrate
class CategoryModel(models.Model):
parentId = models.ForeignKey('self')
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...