skip to Main Content

The problem is that, I want that the authenticated users login into the form. And They can make only 10 forms after the 10 forms message is displayed. I have tried but the message is not displayed.

VIEWS.PY
def create_form(request):

# Creator must be authenticated
if not request. user.is_authenticated:
    return HttpResponseRedirect(reverse("login"))

def create_form(request):
# Assuming the user is already authenticated
logged_in_user = request.user

# Set the maximum allowed forms per creator (in this case, 10)
max_forms_per_creator = 10

# Get the count of forms created by the logged-in user
form_count = Form.objects.filter(creator=logged_in_user).count()

# Check if the user has reached the form creation limit
if form_count >= max_forms_per_creator:
    
   return  render(request, "error/error.html" )     

This program works but it does not show the error message nor show the error HTML page

2

Answers


  1. You can’t have two functions with the same name. How is Django supposed to know which view function to use? My guess is that it’s just picking the first one, which is why the second is never used. Combine them, and that may help:

    def create_form(request):
    
        # Creator must be authenticated
        if not request.user.is_authenticated:
            return HttpResponseRedirect(reverse("login"))
    
        # Assuming the user is already authenticated 
        logged_in_user = request.user
    
        # Set the maximum allowed forms per creator (in this case, 10)
        max_forms_per_creator = 10
    
        # Get the count of forms created by the logged-in user
        form_count = Form.objects.filter(creator=logged_in_user).count()
    
        # Check if the user has reached the form creation limit
        if form_count >= max_forms_per_creator:
           return  render(request, "error/error.html" )
    
        # You need to add here what should happen if the user is NOT authenticated
        # AND the form_count is NOT >= 10
        return render(request, .....)
    
    
    Login or Signup to reply.
  2. I think the problem has something to do with your templating. Certain condition is not met and you will like to redirect back to an error page. Well, tell django the exact app you are working with since error is not the name of the app. e.g . try to restructure your code like this;
    appname
    –template
    –appname
    –errorfolder
    —error.html
    then modify this
    if form_count >= max_forms_per_creator:
    return render(request, "errorfolder/error.html" )
    hope it works .

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search