Reputation: 277
I'm trying to figure out how to redirect to a simple html response page after post in Django rest framework application.
this is how model looks like in web:
Form for file upload:
And after click on post button I have code in views.py that should redirect it on simple html but instead it stays on same page with empty queryset results and I don't know what I'm I doing wrong?
from rest_framework import viewsets
from api.models import UploadImage
from api.serializers import UploadedImageSerializer
from rest_framework import permissions
from rest_framework.parsers import MultiPartParser,FormParser
from sqlite_queries import user_ips
class FileUploadViewSet(viewsets.ModelViewSet):
#create queryset view
permission_classes = (permissions.IsAuthenticated,)
queryset = UploadImage.objects.all()
serializer_class = UploadedImageSerializer
parser_classes = (MultiPartParser, FormParser,)
#after post action get this
def perform_create(self, serializer):
#grab request
image_name = self.request.data['image']
meta_data = self.request.META
username = self.request.user
REMOTE_ADDR = meta_data['REMOTE_ADDR']
#check user status, how many ip's he had over last 24 hours
user_status = user_ips(username)
#if user has more than allowed redirect to html page
if user_status == 400:
print ("how to redirect here?")
#return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
#return HttpResponseRedirect("limit.html")
#if user iz valid continue with upload
serializer.save(user = username, meta_data= meta_data,remote_ip=REMOTE_ADDR)
Upvotes: 1
Views: 4632
Reputation: 403
If I have understand this correctly you don't want that some user overexcites the specific number of logins from different IP address in 24 hours. You can try like this but i suggest you do these steps in virtual environment python.
1.You can delete this part of code from your views.py:
#check user status, how many ip's he had over last 24 hours
user_status = user_ips(username)
#if user has more than allowed redirect to html page
if user_status == 400:
print ("how to redirect here?")
#return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
#return HttpResponseRedirect("limit.html")
2.copy this sqlite_queries.py to Python 3\Lib so it could be used globally if you didn't.
3.Then we want to stop a login from a username that has exceeded number of allowed logins from different IP addresses. And based on the conversation with Fazil Zaid that you just want to do it with Django Rest Framework this is the way you could do it. How we gonna do it?
3.1 You have to open Python 3\Lib\site-packages\django\contrib\auth\forms.py
3.2 Inside you will go to function confirm_login_allowed that is inside class AuthenticationForm and at the start of the start of the function and this lines
from sqlite_queries import user_ips
#check user status, how many ip's he had over last 24 hours
user_status = user_ips(user,r'full_path_to_you_sqlite_db')
#if user has more than allowed redirect to html page
if user_status == 400:
raise forms.ValidationError(
self.error_messages['inactive'],
code='inactive',
)
Now if user matches you sql query and he will be stopped at login with message "This account is inactive.".
3.4 If you want to change error message to some custom text you should go to top of class AuthenticationForm and there you will find dictionary error_messages. Just add your key and value to dictionary and that is it.
Upvotes: 1
Reputation: 6144
Dragon, Assuming that you made this api call using ajax, you can get the response back to the calling ajax function and you can read the status / response and redirect using window.location.
Django rest framework
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Javscript Side
$.ajax({
url: 'link to your django restframe work api',
success: function (response) {
$('#post').html(response.responseText);
},
error: function (jqXHR, exception) {
var msg = '';
if (jqXHR.status === 0) {
msg = 'Not connect.\n Verify Network.';
} else if (jqXHR.status == 400) {
window.location = "limit.html" //window.location = "{% django named url to limit view %}";
} else if (jqXHR.status == 500) {
msg = 'Internal Server Error [500].';
} else {
msg = 'Uncaught Error.\n' + jqXHR.responseText;
}
},
});
You can catch the 400 in the error part. Alternatively you may send a 200 with a custom message and handle the same within ajax success function too.
Hope it helps
Upvotes: 0