Creating a Django App.

Manikandan Prabhakaran
7 min readJun 9, 2020

Django Installation and initial set up:

hello guys... I'm gonna share how to create your own weather app using open weather map API. you need some prerequisites.

  1. Knowledge about Django Installation
  2. Basics of front end technologies like HTML/CSS
  3. Rest API.
  1. Django

Django is a python based web framework which is used for rapid application development . Unlike other technologies, Django is based on MVT pattern. Model-View-Template pattern. which in the sense we use templates to define front end. lets deep dive...

installation :

run the pip command to install Django.

pip install django

Create a new Project:

django-admin startproject mysite

Here mysite is your project name. the startproject command creates a directory structure like this.

mysite/
manage.py
mysite/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
  • The outer mysite/ root directory is a container for your project. Its name doesn’t matter to Django; you can rename it to anything you like.
  • manage.py: A command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py in django-admin and manage.py.
  • The inner mysite/ the directory is the actual Python package for your project. Its name is the Python package name you’ll need to use to import anything inside it (e.g. mysite.urls).
  • mysite/__init__.py: An empty file that tells Python that this directory should be considered a Python package. If you’re a Python beginner, read more about packages in the official Python docs.
  • mysite/settings.py: Settings/configuration for this Django project. Django settings will tell you all about how settings work.
  • mysite/urls.py: The URL declarations for this Django project; a “table of contents” of your Django-powered site. You can read more about URLs in URL dispatcher.
  • mysite/asgi.py: An entry-point for ASGI-compatible web servers to serve your project. See How to deploy with ASGI for more details.
  • mysite/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project. See How to deploy with WSGI for more detail.

from these files, the most important file is your settings file. we are gonna modify it a bit in near future.

The development server:

python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.

June 08, 2020 - 15:50:53
Django version 3.0, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

when you run the command python manage.py runserver you can really see your server(localhost) on http://127.0.0.1:8000/

Creating the weather app

To create your app, make sure you’re in the same directory as manage.py and type this command:

python manage.py startapp myapp

after this command, you can see,

myapp/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py

To create a URLconf in the polls directory, create a file called urls.py. Your app directory should now look like:

myapp/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
urls.py
views.py

In the urls.py file includes the following code:

from django.urls import pathfrom . import viewsurlpatterns = [
path('', views.index, name='index'),
]

The next step is to point the root URLconf at the myapp.urls module. In mysite/urls.py, add an import for django.urls.include and insert an include() in the urlpatterns list, so you have:

from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('myapp/', include('myapp.urls')),
path('admin/', admin.site.urls),
]

Database setup:

Now, open up mysite/settings.py. It’s a normal Python module with module-level variables representing Django settings.

By default, the configuration uses SQLite. If you’re new to databases, or you’re just interested in trying Django, this is the easiest choice. SQLite is included in Python, so you won’t need to install anything else to support your database. When starting your first real project, however, you may want to use a more scalable database like PostgreSQL, to avoid database-switching headaches down the road.

If you wish to use another database, install the appropriate database bindings and change the following keys in the DATABASES 'default' item to match your database connection settings:

  • ENGINE – Either 'django.db.backends.sqlite3', 'django.db.backends.postgresql', 'django.db.backends.mysql', or 'django.db.backends.oracle'. Other backends are also available.
  • NAME – The name of your database. If you’re using SQLite, the database will be a file on your computer; in that case, NAME should be the full absolute path, including filename, of that file. The default value, os.path.join(BASE_DIR, 'db.sqlite3'), will store the file in your project directory.

If you are not using SQLite as your database, additional settings such as USER, PASSWORD, and HOST must be added. For more details, see the reference documentation for DATABASES.

To include the app in our project, we need to add a reference to its configuration class in the INSTALLED_APPS setting. The PollsConfig class is in the polls/apps.py file, so its dotted path is 'polls.apps.PollsConfig'. Edit the mysite/settings.py file and add that dotted path to the INSTALLED_APPS setting. It’ll look like this:

INSTALLED_APPS = [
'polls.apps.PollsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp',
]

add user app name at the end of the installed apps.

Creating an admin user:

python manage.py createsuperuser

Enter your desired username and press enter.

Username: admin

You will then be prompted for your desired email address:

Email address: admin@example.com

The final step is to enter your password. You will be asked to enter your password twice, the second time as a confirmation of the first.

Password: **********
Password (again): *********
Superuser created successfully.

Start the development server:

The Django admin site is activated by default. Let’s start the development server and explore it.

If the server is not running start it like so:

$ python manage.py runserver

Now, open a Web browser and go to “/admin/” on your local domain — e.g., http://127.0.0.1:8000/admin/. You should see the admin’s login screen:

Since translation is turned on by default, if you set LANGUAGE_CODE, the login screen will be displayed in the given language (if Django has appropriate translations).

Enter the admin site:

Now, try logging in with the superuser account you created in the previous step. You should see the Django admin index page:

You should see a few types of editable content: groups and users. They are provided by django.contrib.auth, the authentication framework shipped by Django.

yess!!! we are done with our initial steps. check out my git repository for weather app https://github.com/Maniddarky/Weather_App-Django

2.REST API:

A RESTful API — also referred to as a RESTful web service or REST API — is based on representational state transfer (REST), an architectural style and approach to communications often used in web services development.

REST technology is generally preferred over the more robust Simple Object Access Protocol (SOAP) technology because REST uses less bandwidth, making it more suitable for efficient internet usage.

The REST used by browsers can be thought of as the language of the internet. With cloud use on the rise, APIs are being used by cloud consumers to expose and organize access to web services. REST is a logical choice for building APIs that allow users to connect to, manage and interact with cloud services flexibly in a distributed environment. RESTful APIs are used by such sites as Amazon, Google, LinkedIn and Twitter.

How does it work?

A RESTful API breaks down a transaction to create a series of small modules. Each module addresses a particular underlying part of the transaction. This modularity provides developers with a lot of flexibility, but it can be challenging for developers to design their REST API from scratch. Currently, several companies provide models for developers to use; the models provided by Amazon S3, Cloud Data Management Interface (CDMI) and OpenStack Swift are the most popular.

A RESTful API uses existing HTTP methodologies defined by the RFC 2616 protocol. They use GET to retrieve a resource; PUT to change the state of or update a resource, which can be an object, file or block; POST to create that resource; and DELETE to remove it.

With REST, networked components are a resource the user requests access to — a black box whose implementation details are unclear. All calls are stateless; nothing can be retained by the RESTful service between execution.

To use the weather api open the site https://openweathermap.org/. Then create your account using your email. Now click on API tab and click the api by city name or refer this link https://openweathermap.org/current#name

Then you can subscribe to free membership for this api service. Once you subscribe you will get the api key using which only you can get the data using API. You will get the email about the activation of your api key. Once your key is activated you can start using the api for development.

In this case you have to use this api ‘http://api.openweathermap.org/data/2.5/weather?q={}&units=imperial&appid=YOUR_API_KEY_HERE'

The above api will be used to get the weather data of different cities across the world.

Check out my resume https://manikandan-resume.herokuapp.com/

--

--

Manikandan Prabhakaran

Computer Programming student, ML Intermediate, Django-ist