fferrin
fferrin

Reputation: 908

Create django webapp + REST API endpoints

I'm trying to create a website with a mobile application. I worked with web apps and REST apis separately but with this project I have to combine both.

My question is how do I have to combined this two sides of the projects.

Suposse I have a User model which has Tasks. Currently I have those models in an app called, for example, myRESTapp. But now, I want to be able to edit the tasks and take advantage of Django forms, but I'm not to do another application called webapp and implement forms based on models that are inmyRESTapi. I think it would not be a good practice considering the modularity of applications.

Upvotes: 2

Views: 1840

Answers (1)

masterfloda
masterfloda

Reputation: 3038

You can have multiple apps in one Django project. I usually have one main app that contains the main models and additional apps build on it.

Also, if you want to use apps in different projects, just make them reusable and put them into separate repositories (which you could add as requirements and install with pip).

As for a REST API, check out the great Django Rest Framework. It provides full CRUD functionality and in combination with crispy forms you can even add/modify data with a simple web UI.

An example of how I structured one of my projects:

base                            # basic application
├── __init__.py
├── admin
│   ├── __init__.py
├── ├── filters.py              # custom filters for the Django Admin
│   └── models.py               # enhancements for the Django Admin
├── apps.py
├── db_router.py                # database router to distinguish between default and legacy DB
├── migrations                  # DB migrations (default & legacy)
├── models
│   ├── __init__.py
│   └── default.py              # default models
│   └── legacy.py               # legacy models
├── services
│   ├── __init__.py
│   ├── service1.py
│   ├── service2.py
├── tests.py
manage.py
my_api_app
├── __init__.py
├── apps.py
├── serializers.py              # DRF Serializers
├── services.py                 # specific services for this app
├── tests.py
├── urls.py                     # API URLs
├── views.py                    # DRF ViewSets
my_web_app
├── __init__.py
├── apps.py
├── services.py                 # specific services for this app
├── static
├── ├── css
├── ├── img
├── ├── js
├── templates
├── tests.py
├── urls.py                     # website URLs
├── views.py                    # web views
projectname
├── __init__.py
├── settings
├── ├── __init__.py
│   ├── base.py                 # base settings for all environments
│   ├── dev.py                  # local dev settings (DEBUG, etc)
│   ├── prod.py                 # production settings (default)
│   └── test.py                 # test settings (sqlite DBs for testing)
├── urls.py                     # urlpatterns (API, website & admin) 
├── wsgi.py
requirements
├── base.txt                    # requirements for all environments
├── dev.txt                     # local dev requirements
├── optional.txt                # optional requirements for HTML doc creation
├── prod.txt                    # production requirements
└── test.txt                    # test env requirements
requirements.txt                # default requirements (prod)
scripts
├── post-merge.sh               # githook for deployment on server
├── doc.sh                      # create the code doc
├── coverage.sh                 # create the code doc

So I have my base app, that contains the models and everything that's used in both of the other apps and an app for the API and one for the website.

I come from PHP and Symfony, where I really fell in love with the idea of reusable services. That's how I keep logic out of my views and keep closer to an actual MVC (which is something Django isn't)

Upvotes: 6

Related Questions