Django and Data Collection

Django and Data Collection

Hello again! A few days ago, I posted a curiosity about data science in football. I particularly enjoyed delving deeper into the world of football using the latest research methods.

Today, I will merge my knowledge.

How to use Django for data collection?

I will try to explain all the details, and if there are significant interactions, I might consider recording a lecture on the same content. Let's get started!

Basic context:

Django is a popular web framework for rapid development in Python and can be used to create applications focused on data collection in various ways. Below, I present a general guide on how you can use Django for data collection. I will assume that you already have Django installed in your environment. If not, you can install it using the following command:

Afterward, it would be nice to create an isolated environment to work on your project.


python -m venv venv  # if you want to run directly on your PC, skip this step
        


Installing Django in the environment:

pip install Django  # note that this installs the latest version of Django        


Now, create the project:

django-admin startproject your_project        

After creating the project, it's time to create an app within the project:

cd your_project
python manage.py startapp your_app        

In the settings.py file, look for INSTALLED_APPS, and add your_app. This way, you are informing the Django project of the existence of the your_app app.

Now, define the models for data collection within your_app. Open the models.py file in the your_app directory and define the models and data for collection:

from django.db import models

class Dados(models.Model):
    nome = models.CharField(max_length=100)
    idade = models.IntegerField()
    email = models.EmailField()        

Now, execute migrations to apply these changes (creation) to the database:

python manage.py makemigrations
python manage.py migrate        

In the next step, create a form for data entry. This process of working with forms in Django is quite simple. Create a file called forms.py within the your_app directory and add the following:

from django import forms
from .models import Dados

class DadosForm(forms.ModelForm):
    class Meta:
        model = Dados
        fields = ['nome', 'idade', 'email']
        

Now, work on the views. Open the views.py file in the your_app directory:

from django.shortcuts import render, redirect
from .forms import DadosForm

def coleta_dados(request):
    if request.method == 'POST':
        form = DadosForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('sucesso')
    else:
        form = DadosForm()

    return render(request, 'coleta_dados.html', {'form': form})

def sucesso(request):
    return render(request, 'sucesso.html')
        

The next step is to configure the URLs. In the your_app directory, create a file called urls.py and add the following:


from django.urls import path
from .views import coleta_dados, sucesso

urlpatterns = [
    path('coleta_dados/', coleta_dados, name='coleta_dados'),
    path('sucesso/', sucesso, name='sucesso')
]
        

At the same level as your your_app, create a folder called templates. It should look like this:


your_app
templates        

Within the templates folder, create a template called coleta_dados.html and insert a form:

<form method="POST" action="{% url 'coleta_dados' %}">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Send</button>
</form>        

For the success template, create a second layout called sucesso.html and add a success message:

<h1>Everything went well</h1>        

Now, add these created URLs within the your_app to the main URLs file to avoid confusion. Follow these steps: look for the directory in your project where the file named settings.py is located. In the same directory, there is a file called urls.py, and that's where we'll add the following lines.

It's possibly like this!

from django.contrib import admin
from django.urls import path

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

Add this !

from django.contrib import admin
from django.urls import path, include

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

Everything is configured. Execute:

python manage.py runserver        


Open your browser and enter https://127.0.0.1:8000/coleta_dados/ to access the data collection form.

And done! A simple form to start data collection.

要查看或添加评论,请登录

Diego Gomes的更多文章

社区洞察

其他会员也浏览了