Follow Django Polls Tutorial with Masonite part 1
Tags: masonite, python, django
Published: 2019-02-28 05:59:56

---
title: Follow Django Polls Tutorial with Masonite part 1
published: true
description: 
tags: Masonite, Python, Django
---

# Writing your first ~~Django~~ Masonite app, part 1
Following [tutorial01](https://docs.djangoproject.com/en/2.1/intro/tutorial01/#writing-your-first-django-app-part-1) 

[Github](https://github.com/kentaro0919/masonite_polls)

## check your installation.
In Django, you run the following command to check your Django version.
```sh
$ python -m django --version
```

In Masonite, you run 
```sh
$ craft -v
```

## Creating a project

In Django.
```sh
$ django-admin startproject mysite
```


In Masonite
```sh
$ craft new mysite
```

## The development server
In Django.
```sh
$ python manage.py runserver
```

In Masonite, We need to install before running the server.
[Running the WSGI Server](https://docs.masoniteproject.com/the-craft-command/introduction#running-the-wsgi-server)
[Host and Port](https://docs.masoniteproject.com/the-craft-command/introduction#host-and-port)
```sh
$ craft install
# then now we can run the server.
$ craft serve
```

## Creating the Polls app

Until this point, Django and Masonite were not so different, just running some commands against your code. From this point, the difference will be clear.
running 

In Django.
```sh
$ python manage.py startapp polls
```
In Masonite, we do not need this.
```sh
$ 
```

## Write your first view

In Django.
```python
# polls/views.py
from django.http import HttpResponse


def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")
```

In Masonite. The [Controllers](https://docs.masoniteproject.com/the-basics/controllers) works like "view" in Django.
First We have to add new Controllers.
```sh
$ craft controller Polls
```
This command generates "app/http/controllers/PollsController.py" 

```python
""" A PollsController Module """


class PollsController:
    """PollsController
    """

    def show(self):
        pass
```

Let's edit this as follow


```python
""" A PollsController Module """


class PollsController:
    """PollsController
    """

    def index(self):
        return "Hello, world. You're at the polls index."
```

In Django urls.py handle the request from the user.

```python
# mysite/urls.py
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('polls/', include('polls.urls')),
    path('admin/', admin.site.urls),
]
```

```python
#polls/urls.py
from django.urls import path

from  . import views

urlpatterns = [
    path('', views.index, name='index'),
]
``` 

In Masonite, the route handles the request. By default, you have routes
/web.py in your project.
[Routing](https://docs.masoniteproject.com/the-basics/routing)

```python
"""Web Routes."""

from masonite.routes import Get, Post

ROUTES = [
    Get().route('/', 'WelcomeController@show').name('welcome'),
]

```
The request is "http://localhost:8000/polls/" so we have to handle /polls/
in our route. Masonite handle the HTTP Verbs and match with a controller.

```python
"""Web Routes."""

from masonite.routes import Get, Post

ROUTES = [
    Get().route('/', 'WelcomeController@show').name('welcome'),
    Get().route('/polls/', 'PollsController@index'),
]

```

make sure you are running the server
```sh
$ craft serve -r
```

Then you can access your page.


![In your browser](https://thepracticaldev.s3.amazonaws.com/i/iy2rs78idoaa79m4lswl.png)

We finished successfully the tutorial01 of Django tutorial with Masonite.