We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Instale o Django e o Django REST Framework:
pip install django djangorestframework
Crie um novo projeto Django:
django-admin startproject floodguard cd floodguard
Crie um novo aplicativo Django:
python manage.py startapp monitoring
floodguard/settings.py: Adicione rest_framework e monitoring aos INSTALLED_APPS:
floodguard/settings.py
rest_framework
monitoring
INSTALLED_APPS
INSTALLED_APPS = [ ... 'rest_framework', 'monitoring', ]
floodguard/urls.py: Inclua as URLs do aplicativo monitoring:
floodguard/urls.py
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('api/', include('monitoring.urls')), ]
monitoring/models.py: Defina um modelo para os dados dos sensores e previsões:
monitoring/models.py
from django.db import models class SensorData(models.Model): timestamp = models.DateTimeField(auto_now_add=True) location = models.CharField(max_length=255) water_level = models.FloatField() precipitation = models.FloatField() humidity = models.FloatField() class FloodAlert(models.Model): timestamp = models.DateTimeField(auto_now_add=True) location = models.CharField(max_length=255) alert_level = models.CharField(max_length=50) message = models.TextField()
monitoring/admin.py: Registre os modelos para administração:
monitoring/admin.py
from django.contrib import admin from .models import SensorData, FloodAlert admin.site.register(SensorData) admin.site.register(FloodAlert)
monitoring/serializers.py: Crie serializadores para os modelos:
monitoring/serializers.py
from rest_framework import serializers from .models import SensorData, FloodAlert class SensorDataSerializer(serializers.ModelSerializer): class Meta: model = SensorData fields = '__all__' class FloodAlertSerializer(serializers.ModelSerializer): class Meta: model = FloodAlert fields = '__all__'
monitoring/views.py: Implemente as views para acessar os dados via API:
monitoring/views.py
from rest_framework import viewsets from .models import SensorData, FloodAlert from .serializers import SensorDataSerializer, FloodAlertSerializer class SensorDataViewSet(viewsets.ModelViewSet): queryset = SensorData.objects.all() serializer_class = SensorDataSerializer class FloodAlertViewSet(viewsets.ModelViewSet): queryset = FloodAlert.objects.all() serializer_class = FloodAlertSerializer
monitoring/urls.py: Configure as URLs do API:
monitoring/urls.py
from django.urls import path, include from rest_framework.routers import DefaultRouter from .views import SensorDataViewSet, FloodAlertViewSet router = DefaultRouter() router.register(r'sensor-data', SensorDataViewSet) router.register(r'flood-alerts', FloodAlertViewSet) urlpatterns = [ path('', include(router.urls)), ]
floodguard/settings.py: Configure a base de dados, por exemplo, usando SQLite (por padrão) ou outra base de dados como PostgreSQL se preferir.
Execute as migrações:
python manage.py makemigrations python manage.py migrate
Crie um superusuário para acessar o admin:
python manage.py createsuperuser
Inicie o servidor de desenvolvimento:
python manage.py runserver
Acesse o painel de administração em http://127.0.0.1:8000/admin e a API em http://127.0.0.1:8000/api/.
http://127.0.0.1:8000/admin
http://127.0.0.1:8000/api/
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Instale o Django e o Django REST Framework:
2. Criação do Projeto e do Aplicativo Django
Crie um novo projeto Django:
django-admin startproject floodguard cd floodguard
Crie um novo aplicativo Django:
3. Estrutura do Projeto
floodguard/settings.py
:Adicione
rest_framework
emonitoring
aosINSTALLED_APPS
:floodguard/urls.py
:Inclua as URLs do aplicativo
monitoring
:4. Modelo de Dados
monitoring/models.py
:Defina um modelo para os dados dos sensores e previsões:
monitoring/admin.py
:Registre os modelos para administração:
5. Serializadores e Views
monitoring/serializers.py
:Crie serializadores para os modelos:
monitoring/views.py
:Implemente as views para acessar os dados via API:
monitoring/urls.py
:Configure as URLs do API:
6. Configuração da Base de Dados
floodguard/settings.py
:Configure a base de dados, por exemplo, usando SQLite (por padrão) ou outra base de dados como PostgreSQL se preferir.
7. Migrações e Inicialização
Execute as migrações:
Crie um superusuário para acessar o admin:
Inicie o servidor de desenvolvimento:
8. Teste o Projeto
Acesse o painel de administração em
http://127.0.0.1:8000/admin
e a API emhttp://127.0.0.1:8000/api/
.The text was updated successfully, but these errors were encountered: