Daljit Singh
Daljit Singh

Reputation: 115

Django - Urls are appending to one another

I'm making a web app in django and currently I'm facing this problem. I have a dashboard page and an upload page. There's a button in dashboard page which links to the upload page. but whenever I click the button then the upload page url appends the dashboard page. below is the code:

views.py

from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from .models import Registration
from .forms import UploadForm
from django.urls import reverse

# Create your views here.
def homepage(request):
  return render(request, 'index.html', {})

def dashboard(request):
  posts = Registration.objects.all()
  return render(request, "dashboard.html", {'posts': posts})

def upload(request):
  form = UploadForm()
  return render(request, "upload.html", {'form': form})

def uploadimage(request):
  if request.method == 'POST':
    form=UploadForm(request.FILES['image'], request.POST)
      if form.is_valid():
        pic = request.FILES['image']
        desc = request.POST
        post = Registration(pic='pic', desc='desc')
        post.save()

urls.py

from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from . import views
urlpatterns = [
    path('', views.homepage, name='homepage'),
    path('dashboard/', views.dashboard, name='dashboard'),
    path('upload/', views.upload, name='upload'),
    path('create/', views.uploadimage, name='uploadimage'),
    ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

dashboard.html

 <div class="profile">
      <div class="left-s col s12 m3 l3">
        <div class="profile-overview">
            <img src="{%static 'images/group.jpg' %}" alt="profile-pic" class="circle responsive-img">
            <p>Daljit Singh</p>
            <p>Lorem ipsum dolor sit amet.</p>
        </div>
        <hr>
        <div class="container">
            <ul>
                <li><a href="#">About</a></li>
                <li><a href="#">Friends</a></li>
                <li><a href="#">Photos</a></li>
                <li><a href="#">Likes</a></li>
            </ul>
        </div>
        <hr>
        <button><a href="upload/">Upload post</a></button>
      </div>

error:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/dashboard/upload/
Using the URLconf defined in main.urls, Django tried these URL patterns, in this order:
[name='homepage']
dashboard/ [name='dashboard']
upload/ [name='upload']
create/ [name='uploadimage']
^media\/(?P<path>.*)$
The current path, dashboard/upload/, didn't match any of these.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

Help would be appreciated.

Upvotes: 3

Views: 1748

Answers (1)

Chetan Patel
Chetan Patel

Reputation: 782

It should be - <a href="/upload/">Upload post</a>

If you enter '/' (/upload/) before the path it will append to the base URL and if '/' (upload/) doesn't exist then it will append to the existing path.

Or the Different way suggested in the comment -

<a href="{% url 'upload' %}">Upload post</a>

Upvotes: 3

Related Questions