too many redirects error, mod_wsgi apache and django (python-venv) - Stack Overflow

admin2025-04-16  4

I have a problem, with django and apache (mod_wsgi), I get the browser error "too many redirects". Here my files and apache configuration

project tree

├── db.sqlite3
├── djangoProject
│   ├── asgi.py
│   ├── __init__.py
│   ├── __pycache__
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── manage.py
└── sito
    ├── admin.py
    ├── apps.py
    ├── __init__.py
    ├── migrations
    ├── models.py
    ├── __pycache__
    ├── templates
    ├── tests.py
    ├── urls.py
    └── views.py

sito/urls.py

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('display_config/', views.display_config, name='display_config'),
]

djangoProject/urls.py

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


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

views.py

from django.http import HttpResponse
from django.shortcuts import render
from django.template import loader

from .models import PingAction, Config

from . import views 

def index(request):
    return HttpResponse("Hello, world.")

def display_config(request):
    config_list = Config.objects.all()
    template = loader.get_template("display_config.html")
    context = {
        "config_list": config_list,
    }
    return HttpResponse(template.render(context, request))

httpd.conf

WSGIScriptAlias /app-web /mnt/data/Workspace/app/app-web/djangoProject/djangoProject/wsgi.py
WSGIPythonHome /mnt/data/Workspace/app/app-web/app-web-env
WSGIPythonPath /mnt/data/Workspace/app/app-web/djangoProject


<Directory "/mnt/data/Workspace/app/app-web/djangoProject">
<Files wsgi.py>
   Require all granted
</Files>
</Directory>

When I navigate to http://localhost/app-web/sito/display_config the browser show me the "too many redirects" error. That in differents browsers.

If I use

python manage.py runserver 

and navigate to http://127.0.0.1:8000/ or http://127.0.0.1:8000/display_config all works correcty.

Can somebody help me please to understand what's wrong? Thank a lot

Update: I add an image of the developer tools bar when trying to load the page (from previous code I change app-web to melons-web, this is not the problem). I want also specify that this happen only on my linux machine, the same code in Windows works perfectly

I have a problem, with django and apache (mod_wsgi), I get the browser error "too many redirects". Here my files and apache configuration

project tree

├── db.sqlite3
├── djangoProject
│   ├── asgi.py
│   ├── __init__.py
│   ├── __pycache__
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── manage.py
└── sito
    ├── admin.py
    ├── apps.py
    ├── __init__.py
    ├── migrations
    ├── models.py
    ├── __pycache__
    ├── templates
    ├── tests.py
    ├── urls.py
    └── views.py

sito/urls.py

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('display_config/', views.display_config, name='display_config'),
]

djangoProject/urls.py

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


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

views.py

from django.http import HttpResponse
from django.shortcuts import render
from django.template import loader

from .models import PingAction, Config

from . import views 

def index(request):
    return HttpResponse("Hello, world.")

def display_config(request):
    config_list = Config.objects.all()
    template = loader.get_template("display_config.html")
    context = {
        "config_list": config_list,
    }
    return HttpResponse(template.render(context, request))

httpd.conf

WSGIScriptAlias /app-web /mnt/data/Workspace/app/app-web/djangoProject/djangoProject/wsgi.py
WSGIPythonHome /mnt/data/Workspace/app/app-web/app-web-env
WSGIPythonPath /mnt/data/Workspace/app/app-web/djangoProject


<Directory "/mnt/data/Workspace/app/app-web/djangoProject">
<Files wsgi.py>
   Require all granted
</Files>
</Directory>

When I navigate to http://localhost/app-web/sito/display_config the browser show me the "too many redirects" error. That in differents browsers.

If I use

python manage.py runserver 

and navigate to http://127.0.0.1:8000/ or http://127.0.0.1:8000/display_config all works correcty.

Can somebody help me please to understand what's wrong? Thank a lot

Update: I add an image of the developer tools bar when trying to load the page (from previous code I change app-web to melons-web, this is not the problem). I want also specify that this happen only on my linux machine, the same code in Windows works perfectly

Share edited Feb 4 at 16:27 luna80 asked Feb 2 at 5:59 luna80luna80 1533 silver badges16 bronze badges 16
  • Please share your project structure. I’m not sure how it would work correctly even with python manage.py runserver. Your urls.py look like they’re indeed the cause of the problem. – raphael Commented Feb 2 at 18:34
  • I edited my post with the tree of the project structure, and some others change, but the issue is still the same. thanks – luna80 Commented Feb 3 at 5:36
  • Sorry, I'm out of ideas. The changes you made to urls.py should have made the difference. All I can suggest is trying to restart the service, sudo systemctl restart httpd.service. – raphael Commented Feb 3 at 15:24
  • 1 @luna80, I tried running the project through Docker, with all your files and the Apache config you provided. And, expectedly, the problem did not reproduce, everything works. Do you have any customizations of your own in settings.py, if so, please attach them to the question. Have you tested incognito mode? Perhaps you have changed the Apache configuration before and have mod_rewrite enabled?, as again, with pure Apache, in docker the problem is not reproduced? Try as a last option to completely reinstall Apache. – Serhii Fomenko Commented Feb 6 at 13:12
  • 1 @SerhiiFomenko mod_rewrite was the problem!!! I don't know I have it enabled! I don't remeber I do this! Maybe a defalt value? I disable it and now all works perfectly! Thank you very much!! Can I ask you to put this solution as an answer so I can give you the bounty? – luna80 Commented Feb 8 at 6:03
 |  Show 11 more comments

2 Answers 2

Reset to default 1 +50

I assume that your problem is related to the Apache settings, on your linux machine, you probably have mod_rewrite enabled which is causing the redirection problem.

I think you have 2 URLs that come to (http://localhost/app-web/display_config) bt clearing this line in the main URL file the problem may be solved path('display_config/', include('sito.urls')),

转载请注明原文地址:http://anycun.com/QandA/1744808822a87912.html