Not able to perform post request django+react - python

#accounts.models.py:
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from words import models as word_models
# Create your models here.class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete = models.CASCADE)
last_visited_word = models.ForeignKey(word_models.Word, default=4760, on_delete = models.CASCADE)
def create_user_profile(sender, instance, created, **kwargs):
if created:
profile, created = UserProfile.objects.get_or_create(user=instance)
post_save.connect(create_user_profile, sender=User)
#words/models.py
from django.db import models
from django.conf import settings
# Create your models here.
class Word(models.Model):
word = models.TextField(blank=True, null=True)
meaning = models.TextField(blank=True, null=True)
#accounts/serializers.py:
from rest_framework import serializers
from .models import UserProfile
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = UserProfile
fields = '__all__'
#accounts/views.py:
#api_view(['POST'])
##authentication_classes([SessionAuthentication, BasicAuthentication])
#permission_classes([IsAuthenticated])
def userprofile_create_view(request, *args, **kwargs):
user_id = request.data.get('user')
try:
instance = UserProfile.objects.get(user_id=user_id)
except UserProfile.DoesNotExist:
instance=None
serializer = UserSerializer(instance, data=request.data)
if serializer.is_valid(raise_exception=True):
# create or update data
serializer.save()
return Response(serializer.data)
So Basically I have to update the last_visited_word of the user when the user visits that particular id. I am not able to do the "post" method to update last_visited_word. Api calling is done using axios. I am using React as my frontend. When I try to use the post request, it says a bad request. BTW react and django are connected as I am able to do a GET request successfully.

first of the question is kind of incomplete, but from what I can understand you need to configure your settings.py to allow post request from your react localhost. Also finding a way around Django and react can be quite tricky.

check if the api is working as expected. also in the django setting enable cors in middleware and whitelist the server on which the react is running.

Related

Django Friend Request system

I am trying to make a friend request system with Django for a cat app, and I am having a problem. I have models to track the friends and the friend request. In the views I have a redirect view with a try except clause that creates a new instance of the friend request model. Then the friend request will be shown to whoever it was sent to, and they will accept or decline it. The problem I have is i don't know how to grab the info about the user to whom the friend request is sent. Any help would be appreciated. Here is the link to my project repository https://github.com/codewiz9/chatter
modles.py
from django.db import models
from django.utils.text import slugify
from django.contrib.auth import get_user_model
User = get_user_model()
# Create your models here.
class Chat(models.Model):
messages = models.TextField(blank=True, max_length=2000, null=False),
date = models.DateTimeField(blank=False, editable=False),
slug = models.SlugField(allow_unicode=True, unique=True,),
friends = models.ManyToManyField(User, through='Friend_List'),
class Friend_List(models.Model):
friend_name = models.ForeignKey(User, related_name='name', on_delete=models.CASCADE),
is_friend = models.BooleanField(default=False),
def __str__(self):
return self.user.username
class Friend_Info(models.Model):
friend_name = models.ForeignKey(User, related_name='name', on_delete=models.CASCADE),
slug = models.SlugField(allow_unicode=True, unique=True,),
class Friend_Request(models.Model):
yes_or_no = models.BooleanField(default=False),
friends = models.ManyToManyField(User, through='Friend_List'),
slug = models.SlugField(allow_unicode=True, unique=True,),
Views.py
from django.shortcuts import render
from django.urls import reverse
from django.views import generic
from .models import Chat, Friend_List, Friend_Info, Friend_Request
# Create your views here.
###Genral###
class Dashbord(generic.TemplateView):
#This classs will have the list of all the users chats
models = Chat, Friend_List
template_name = 'chat_app/dashbord.html'
###Friends###
class Friend_Dashbord(generic.ListView):
#This view will allow users to see thire friends and see thire friend requests and this will contain the button to add new friends
models = Friend_Info, Friend_List
template_name = 'chat_app/friend_dashbord.html'
class Friend_Request(generic.RedirectView):
#This is the form for sending requests S=sent
models = Friend_Request, Friend_list, Friend_Info
def get(self, request, *args, **kwargs):
friend = get_object_or_404(Friend_Info, slug=self.kwargs.get("slug"))
try:
Friend_Request.objects.create(friends=)
class Find_Friends(generic.FormView):
#this will be the page where you can serch for friends
models = Friend
template_name = 'chat_app/dashbord.html'
###Chat###
#the chat portion of the app will be handeled in two parts one will the the form to send the chat and one will be the
#list of all the chats the form view will be inclued on the Chat_list template
class Chat_List(generic.ListView):
#This will be the list of all the chats sent and resived
models = Chat
template_name = 'chat_app/dashbord.html'
class Chat_Form(generic.FormView):
models = Chat
template_name = 'chat_app/dashbord.html'
This is how I would implement such a model:
class FriendRequest(models.Model):
# create a tuple to manage different options for your request status
STATUS_CHOICES = (
(1, 'Pending'),
(2, 'Accepted'),
(3, 'Rejected'),
)
# store this as an integer, Django handles the verbose choice options
status = models.IntegerField(choices=STATUS_CHOICES, default=1)
# store the user that has sent the request
sent_from = models.ForeignKey(User, ..., related_name="requests_sent")
# store the user that has received the request
sent_to = models.ForeignKey(User, ..., related_name="requests_received")
sent_on = models.DateTimeField(... # when it was sent, etc.
Now, you will notice that the two user ForeignKey fields have related_name attributes, these are reverse accessors and are how you can get related model objects.
Say you have a given user object, you can get all of the friend requests they have sent and received using these queries:
# Friend requests sent
user.requests_sent.all()
# Friend requests received from other users
user.requests_received.all()
Those provide you with querysets of other users that you can then iterate over and access as needed.

Problem sending information using POST method in django rest framework

Hi i'm knew in django rest framework:
views.py:
from django.shortcuts import render
from rest_framework import status
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import Book
from .serializers import BookModelSerializers
# for posting data
class PostModelData(APIView):
def post(self, request):
serializer = BookModelSerializers(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
models.py:
from django.db import models
from datetime import datetime
class Book(models.Model):
name = models.CharField(max_length=150, blank=True)
store_name = models.CharField(max_length=50, blank=True)
description = models.TextField(blank=True)
image = models.ImageField(
default='', upload_to='store_image/', null=True, blank=True)
fav = models.BooleanField(default=False, blank=True)
created_at = models.DateTimeField(default=datetime.now())
def __str__(self):
return self.name
urls.py:
from django.urls import path
from .views import GetAllData, Getfavdata, UpdateFavData, PostModelData
urlpatterns = [
path('getalldata/', GetAllData.as_view()),
path('getfavdata/', Getfavdata.as_view()),
path('updatefavdata/<int:pk>/', UpdateFavData.as_view()),
path('postmodeldata/', PostModelData.as_view()),
]
serializers.py:
from rest_framework import serializers
from .models import Book
class BookModelSerializers(serializers.ModelSerializer):
class Meta:
model = Book
fields = '__all__'
When I post ('name','store_name','description','fav') with postman, the data that is stored is without details.
I wanted to know what the problem is?
I also removed (blank = True) in models.py, nothing is saved.
enter image description here
Ah, I looked at the picture your attached and found what's wrong.
You're sending your data as GET parameters in the URL. E.g. https://localhost:8000?name=lorem&storename=lorem2.
However, you have defined your view method to expect a POST request:
class PostModelData(APIView):
#Use this method for POST requests
def post(self, request):
pass
#Use this method for GET requests
def get(self, request):
pass
In this case though, when you want to receive data from the user and save it POST request is the correct choice, you just put your data in the wrong place.
Instead do this:
1: Change your code in your view
class PostModelData(APIView):
def post(self, request):
#If you want to send MULTIPLE entries per request
serializer = BookModelSerializers(data=request.data, many=True)
#If you want to send ONE entry per request
serializer = BookModelSerializers(data=request.data, many=False)
2: Go to Postman and do the following steps.
3: Remove all parameters from url so you just have http://localhost:8000/api/v1/postmodeldata/
4: Right next to the URL, make sure you have POST selected.
5: Under the URL bar, click on Body.
6: In the radiobuttons change to raw. And in the dropdown menu right next to it select JSON.
7: In the textarea write something like in this picture:
Example request
8: Click Send and you should get a 201 response from the server.
class BookModelSerializers(serializers.ModelSerializer):
class Meta:
model = Book
fields = '__all__'
def create(self, validated_data):
book = Book.objects.create(**validated_data)
book.save()
return book

How to add a registered user to the User model (within Authentication and authorization section) in Django?

A newbie here. This is a Django related question.
How can I save a newly registered user to the User Model (auth.model)? Currently, the only account which is seen inside the admin panel -- under Users (Authentication and Authorization section) is the superuser (aka me).
I am using DRF (Rest framework) in order to register a user and not an HTML form.
models.py:
class Register(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
username = models.CharField(max_length = 100)
email = models.EmailField(max_length = 100)
password = models.CharField(max_length = 100)
def __str__(self):
return self.name
views.py:
class RegisterView(APIView):
def post(self, request, format=None):
serializer = RegisterSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response("Thank you for registering", status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
serializers.py:
from rest_framework import serializers
from .models import Register
class RegisterSerializer(serializers.ModelSerializer):
class Meta:
model = Register
fields = ['username', 'email', 'password']
When registering a new user via POSTMAN, the data is saved within the Register model (which is fine) but my issue is that it's not seen within the Users model.
Any feedback is highly appreciated. Thank you.

In Django, how to add creator of a group to that group instantly?

I'm creating a 'social' app in Django, where the users can create groups (Alliances) and others can join these groups. User Profiles and Alliances are connected through Membership model. I´d like the creator of such group to be a member of it instantly.
I'm inexperienced in Django, in fact, this is my first project. But I figured that this maybe could be solved by signals?
My user Profile model:
class Profile(models.Model):
...
user = models.OneToOneField(User, on_delete=models.CASCADE)
alliances = models.ManyToManyField('Alliance', through='Membership')
...
My Alliance model:
class Alliance(models.Model):
...
name = models.CharField(max_length=10, unique=True)
members = models.ManyToManyField('Profile', through='Membership')
...
My Membership model:
class Membership(models.Model):
...
profile = models.ForeignKey('Profile', on_delete=models.CASCADE)
alliance = models.ForeignKey('Alliance', on_delete=models.CASCADE)
...
The solution I figured could work (using signals) would look something like this:
#receiver(post_save, sender=Alliance)
def create_membership(sender, instance, created, **kwargs):
if created:
Membership.objects.create(profile=???, alliance=instance)
Where the '???' should be the creators profile. I'll be really glad for any help.
A signal has no access to the request that triggered it, since it is not said that the trigger is a HTTP request in the first place. You could try to inspect the traceback, but that is a hacky solution that will probably generate more trouble than it is worth.
You will need to do that in the form or in the view where you create the group. For example in the view, you can add this with:
from django.views.generic import CreateView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponseRedirect
from app.models import Alliance, Membership
class AllianceCreateView(LoginRequiredMixin, CreateView):
model = Alliance
def form_valid(self, form):
self.object = form.save()
Membership.objects.create(
profile=self.request.user.profile,
alliance=self.object
)
return HttpResponseRedirect(self.get_success_url())
Note that the modeling is quite strange. Normally you do not specify two ManyToManyFields. Django will automatically add the second one. You thus can specify in your Profile:
class Profile(models.Model):
# ...
user = models.OneToOneField(User, on_delete=models.CASCADE)
alliances = models.ManyToManyField(
'Alliance',
through='Membership',
related_name='members'
)

Django Rest Framework Create REST API only for executions

I'm working on a django project in which I need to build a rest API for some executions, I don't have much concerns about models as I only need to perform executions on the base of user's inputs/calls.
Here's my scenario:
The Major thing is the Deployment ( An App in my project )
On a get request user will get a list of all it's
deployments(objects).
A user will send a POST request to /deployments along with the complete
object as:
{
"deployment_name": "dep4",
"credentials": "cre4",
"project_name": "pro4",
"project_id": "004",
"cluster_name": "clus4",
"zone_region": "zon4",
"services": "Single",
"configuration": "conf4",
"routing": "route4" }
then I need to use this data, validate and perform some execution on the base of this data. For example, I will make deployments on a cloud platform with the help of this information by using third-party APIs.
I'm really confused by the documentation of DRF, especially about serializers.
Update: Currently, How I'm trying to do the POST:
From apiview.py
class DeploymentsList(APIView):
queryset = DeploymentOnUserModel.objects.all()
def post(self, request):
print(request.data)
DeploymentOnUserModel.objects.create(
deployment_name=request.data['deployment_name'],
credentials=request.data['credentials'],
project_name=request.data['project_name'],
project_id=request.data['project_id'],
cluster_name=request.data['cluster_name'],
zone_region=request.data['zone_region'],
services=request.data['services'],
configuration=request.data['configuration'],
routing=request.data['routing'],
)
return Response(request.data)
So, how can I validate the incoming data/request?
Here's my Deployment Model:
class DeploymentOnUserModel(models.Model):
deployment_name = models.CharField(max_length=256, )
credentials = models.TextField(blank=False)
project_name = models.CharField(max_length=150, blank=False)
project_id = models.CharField(max_length=150, blank=True)
cluster_name = models.CharField(max_length=256, blank=False)
zone_region = models.CharField(max_length=150, blank=False)
services = models.CharField(max_length=150, choices=services)
configuration = models.TextField()
routing = models.TextField()
def save(self, **kwargs):
if not self.id and self.services == 'Multiple' and not self.routing and not self.configuration:
raise ValidationError("You must have to provide routing for multiple services deployment.")
super().save(**kwargs)
From urls.py:
app_name = 'deployments'
urlpatterns = [
path('deployments/', apiview.DeploymentsList.as_view(), name='deployment_list'),
path('deployments/<int:pk>', apiview.DeploymentDetail.as_view(), name='deployment_detail')
]
Which approach and things from DRF I should use to implement my API.
You will probably want to implement DRFs ModelSerializer and ModelViewSet such that you can easily reuse the Django model you already have.
Your serializer could be like this (e.g. in serializers.py):
from rest_framework import serializers
from .models import DeploymentOnUser
class DeploymentOnUserModelSerializer(serializers.ModelSerializer):
class Meta:
model = DeploymentOnUser
fields = (deployment_name, credentials, )
You should add your own validation here, much like you would do with Django forms.
The viewset could be like this (e.g. in views.py):
from rest_framework import viewsets
from rest_framework.response import Response
from .models import DeploymentOnUserModel
from .serializers import DeploymentOnUserModelSerializer
class DeploymentOnUserViewSet(viewsets.ModelViewSet):
queryset = DeploymentOnUserModel.objects.all()
serializer_class = DeploymentOnUserModelSerializer
def create(self, request, *args, **kwargs):
"""overwrite this for extra actions"""
serializer = self.serializer_class(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data)
Depending on your situation, you might even need to overwrite create -- this just shows how you could do it.
The ModelViewSet and ModelSerializer remove much of the boilerplate code. However, if you haven't used DRF before, it doesn't hurt to first go through the tutorial
Don't forget to also register the viewset in urls.py:
from django.conf.urls import url, include
from rest_framework import routers
from .views import DeploymentOnUserViewSet
router = routers.DefaultRouter()
router.register('deployments', DeploymentOnUserViewSet)
urlpatterns = [
# other patterns also go here
url('', include(router.urls)),
]
You could then test your API by doing e.g. a POST call on /deployments/.
For unit testing, I mostly use this pattern:
from rest_framework.test import APIRequestFactory
# Create a POST request, at the root
request = APIRequestFactory().post('/')
response = DeploymentOnUserViewSet.as_view({'post': 'create'})(request)
assert response.status_code == 200

Categories