How to Build a RESTful API using the Django REST Framework

The Django REST Framework (DRF) allows you create REST based APIs quickly and simply, providing a range of features such authentication, error handling, serialization and full CRUD  (Create Replace Update Delete) based operations to your database. Within this article will look at how to create a very basic API. The API will take in a … Read more

Django+MongoEngine Update_or_Create Action

BuiltIn ORM Django provides a convenient method when needing to update a model instance, but create if it does not exist. The method is shown below, update_or_create(defaults=None, **kwargs) However MongoEngine replaces the builtin object-relational mapper (ORM). Because of this an alternative is required. MongoEngine Within MongoEngine the modify method can be used to achieve the … Read more

How to Configure Celery within Django

Celery is an open source asynchronous task queue/job queue based on distributed message passing[1]. Within this article we will provide the configuration steps requiring for installing celery within Django. Our tutorial Example Broker Within this example we will use Redis as the broker. Celery uses a broker to pass messages between your application and Celery … Read more

Django Application Design Framework

app-design

Components MODULES Modules should be installed via pip, including modules from github. To install modules from github the following article describes the steps involved. Structure projectx |– app1 | |– __init__.py | |– models.py | |– views.py |– app2 | |– __init__.py | |– models.py | ‘– views.py |– appx | |– __init__.py | |– … Read more

Django Quick Reference

Quick Reference Models Query Description Event.objects.filter(appid=”example”) filter for appid eq example Event.objects..exclude(username=”rick”) show everything not excluding username eq rick Event.objects.filter(appid=”example”).exclude(user__in=[“bob”,”fred”]) include appid eq example and exclude user bob or fred Upgrade.objects.filter(device_number=’device_numberooo’).values() Display as dict() Management Commands Commands Description  ./manage.py dumpdata updowngrade –indent 2       http://stackoverflow.com/questions/14115318/create-django-model-or-update-if-exists 

Django 1.5 – ‘url’ requires a non-empty first argument.

Issue When running Django 1.5.1 you may observe the following error, NoReverseMatch at /‘url’ requires a non-empty first argument. The syntax changed in Django 1.5, see the docs. Solution This issue can occur due to changes in the url tag syntax. More information on this can be found at  https://docs.djangoproject.com/en/dev/releases/1.5/. The correct syntax for url … Read more

Django – How do I create a custom login page ?

Within this article we will look at how to permit only authenticated users to a view via the use of a custom login form. SETTINGS First of all a few changes need to be made to the settings.py file. Such as + ‘django.contrib.auth.middleware.AuthenticationMiddleware’ to MIDDLEWARE_CLASSES + ‘django.contrib.auth’ and ‘django.contrib.contenttypes’to INSTALLED_APPS Once done update your database by … Read more

Django – How can I pass a string from a URL to a view ?

Within Django the are 2 ways of using a URL parameter within a view. They are via URLConf or by using request.GET. URLConf With this method the URLConf file is configured to define your URL parameter via the use of regex. Say that we have a URL that is ‘www.domain.co.uk/user=value’ and we want to grab … Read more

How do I use AJAX along side Django ?

Introduction Within this example we will show you a simple example of how to integrate AJAX with Django. AJAX (Asynchronous JavaScript and XML) is a technique that provides the ability to update parts of a web page, without reloading the whole page. Now days,  typically JSON is the preferred method over XML due to JSON`s … Read more

How do I configure Django to serve my Robots.txt file ?

Within this article we will look at how to configure Django to serve your ‘robots.txt’ file. Though there are a number of methods available to achieve this, this example we will only look at the Django urls.py method. In that only the the urls.py file within your Django project will need to be changed. Example … Read more

Serve Multiple Domains from a Single Django Project

Introduction Within this article we will be showing you the necessary steps required to serve multiple domains from within a single Django project. This example will based upon a project named ‘myproject’ and 2 domains named ‘domain1’ and ‘domain2’. Below shows the file structure. /opt/ `– django     |     `– myproject         |– __init__.py … Read more

How do I configure Apache to serve my Django website ?

Summary In order for Apache to serve your Django website WSGI is configured. The Web Server Gateway Interface (WSGI) defines a simple and universal interface between web servers and web applications or frameworks for the Python programming language (wikipedia). Note : The following steps are based on a Linux Centos based distro. The following steps within … Read more

Django – Static Files

Static Files is a Django app that helps with serving static content. This tutorial is meant as a short and quick guide on how to configure your Django project to serve static based content.To achieve this we will configure Django (via settings.py) and Apache. Our example will be based on serving static content from the … Read more

Django – CSRF verification failed. Request aborted.

Issue When trying to access your Django site within CSRF configured you receive the following via a Forbidden (403) HTTP error message: CSRF verification failed. Request aborted.No CSRF or session cookie. Solution In my scenario I found that the order of settings.MIDDLEWARE_CLASSES was incorrect. Below shows you an example settings.py MIDDLEWARE_CLASSES = (    ‘django.middleware.csrf.CsrfViewMiddleware’,    ‘django.middleware.common.CommonMiddleware’,    … Read more

Want to become a Django expert?

Here is our hand-picked selection of the best courses you can find online:
The Complete Web Development Bootcamp course
Django Practical Guide course
Django Full Stack Developer Bootcamp
and our recommended certification practice exams:
AlphaPrep Practice Tests - Free Trial