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 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 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)
django DRF API Views
REST framework provides two wrappers you can use to write API views.
- The @api_view decorator for working with function based views.
- The APIView class for working with class-based views.
These wrappers provide a few bits of functionality such as making sure you receive Request instances in your view, and adding context to Response objects so that content negotiation can be performed.
The wrappers also provide behaviour such as returning 405 Method Not Allowed responses when appropriate, and handling any ParseError exception that occu...
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 adding or removing a capturing group in the middle of the regex upsets the numbers of all the groups that follow the added or removed group.
Python's re module was the first to offer a solution: named capturing groups and named backreferences.
(?P<name>group)
captures the match of group into t...