Django Page not found (404) - The current path, Didn't match any of these

Viewed 25

I am trying to add a clinic from a users profile, However, i am getting a 404 page not found and i noticed that a single quote is getting appended to the URL:

http://127.0.0.1:8000/profile/'/clinic/add_clinic/

Any help would be appreciated.

Below are my Project URLS:

urlpatterns = [
    # django admin
    path('admin/', admin.site.urls),

    # User management
    path('accounts/', include('allauth.urls')),



    # local apps
    path('', include('pages.urls')), # new
    path('clients/', include('clients.urls')), # new
    path('behaviors/', include('behaviors.urls')),
    path('clientsessions/', include('clientsessions.urls')),
    path('profile/', include('users.urls')),
    path('clinic/', include('clinic.urls')),
]   + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Here are URLs from the users app

urlpatterns = [
    path('', views.profile, name='profile'),
]

Here is the profile view

@login_required
def profile(request):
    if request.method == 'POST':
        user_form = UpdateUserForm(request.POST, instance=request.user)
        profile_form = UpdateProfileForm(request.POST, request.FILES, instance=request.user.profile)

        if user_form.is_valid() and profile_form.is_valid():
            user_form.save()
            profile_form.save()
            messages.success(request, 'Your profile is updated successfully')
            return redirect(to='users-profile')
    else:
        user_form = UpdateUserForm(instance=request.user)
        profile_form = UpdateProfileForm(instance=request.user.profile)

    return render(request, 'users/profile.html', {'user_form': user_form, 'profile_form': profile_form})

Here are the URLs rom the clinic app

urlpatterns = [
    path('clinic/', views.clinic, name='clinic'),
    path('add_clinic/', views.add_clinic, name='add_clinic'),
    
]

And here is the add_clinic view

@login_required
@allower_users(allowed_roles=['admin'])
def add_clinic(request):
    if request.method == 'POST':
        name = request.POST.get('name')

        if name:
            name = Clinic.objects.create(name=name, created_by=request.user)
            clinic.members.add(request.user)
            clinic.save()

            userprofile = request.user.profile
            userprofile.active_clinic_id = clinic.id
            userprofile.save()

            return redirect(to='users-profile')
    return render(request, 'clinic/add_clinic.html')

Here is the profile.html snippet where im trying to navigate to the add_clinic view

<div class="row">
    <!-- Profile Header card -->
    <div class="col-sm-4">
        <div class="card">
            <div class="card-body">
                <img class="rounded-circle account-img" src="{{ user.profile.avatar.url }}" style="cursor: pointer; margin-bottom: 1rem;"/>
                <h5 class="card-title">{{ user.first_name }} {{ user.last_name }}</h5>
                <a href="'{% url 'clinic:add_clinic' %}">Create Clinic</a>
                <div>
                    <p><a href="{% url 'account_logout' %}">Log Out</a></p>
                </div>
            </div>
        </div>
    </div>
1 Answers

It seems that your link is written incorrectly, rewrite this:

<a href="'{% url 'clinic:add_clinic' %}">Create Clinic</a>

as:

<a href="'{% url 'add_clinic' %}">Create Clinic</a>

That should do it.

Related