2badatcoding
2badatcoding

Reputation: 115

Django duplicated url request: "home/home/'

I have code that works perfectly on localhost, but it is giving an error when placed on the server.

The point is that when I upload a file, it returns "base.com/home/home" (home twice = 404) instead of just "base.com/home" or the destination of the redirect.

Template:

<form method="post" enctype="multipart/form-data">
    {% csrf_token %}
    <input type="file" name="myfile">
    <button type="submit">Upload</button>
</form>

View:

def login_page(request):
    if request.user.is_authenticated:
        return redirect('base-home')
    form = UserLoginForm(request.POST or None)
    nextr = request.GET.get('next')
    if form.is_valid():
        username = form.cleaned_data.get('username')
        password = form.cleaned_data.get('password')
        user = authenticate(username=username, password=password)
        login(request, user)
        if nextr:
            return redirect(nextr)
        return redirect('base-home')
    return render(request, 'base/login.html', {'form': form})
    
@login_required    
def home(request):
    if request.method == 'POST':
        return redirect('base-temp', 1)
    return render(request, 'base/home.html', {'test': 'test'})

Url:

urlpatterns = [
    path('', views.login_page, name='base-login'),
    path('logout/', views.logout_page, name='base-logout'),
    path('home/', views.home, name='base-home'),
    path('temp/<int:pk>/', views.temp, name='base-temp')
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Project settings:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

All other pages, media, statics are working fine. Only the post in this view.

I tried using action with ".", "/home/" and "{% url 'base-home'%}", but they all give the same 404 for duplicating the home url.

I also tried to create a separate view to handle the upload, but the error became "home/upload" instead of "upload".

I saw some similar questions in the stackoverflow, but I didn't find an answer that would work for me.

Any suggestion?

EDIT:

It works if I change the template to:

<form method="post">
    {% csrf_token %}
    <textarea name="text"></textarea>
    <button type="submit">Send</button>
</form>

And the view to:

def home(request):
    if request.method == 'POST':
    return redirect('base-temp', 2)
    return render(request, 'base/home.html', {'test': 'test'})

I'm not using django's forms.py.

That's it. Nothing else. Only the imports and a simple function view/template for base-temp.

Edit 2: Added more details about the views.

SOLUTION:

In case anyone else has the same problem, I leave the solution here, based on Maxwell O. Oyaro's answer.

I changed the home route to the empty path of the app urls.py:

urlpatterns = [
    path('', views.home, name='base-home'),
    path('logout/', views.logout_page, name='base-logout'),
    path('temp/<int:pk>/', views.temp, name='base-temp')
]
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Then, I moved login path to the project urls.py:

...
from base import views as base_views
urlpatterns = [
    path('admin/', admin.site.urls),
    path('login/', base_views.login_page, name='login'),
    path('', include('base.urls'))
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Upvotes: 0

Views: 919

Answers (1)

Maxwell O. Oyaro
Maxwell O. Oyaro

Reputation: 26

Check if your home is defined globaly for the django project or it is for the app itself.

Putting templates globally for the project and for the app alone can be confusing. If you have a templates for the project with home and you have another home template in the app calle home, it may not give you what you expect.

Just check keenly.

Upvotes: 1

Related Questions