NoReverseMatch Django url issue

Multi tool use
Multi tool use


NoReverseMatch Django url issue



I am trying to implement a typical url in my django project however i keep getting the error meassage. I have checked my urls, views and html code where i have passed in the KWARGS. I have no clue what went wrong here please help?



The home.html uses user_profile_detail in the template



userprofile/home.html


<div class="sidebar-fixed position-fixed side_nav_bar ">

<a class="logo-wrapper waves-effect">
<img src="/" class="img-fluid" alt="">
</a>

<div class="list-group list-group-flush">
**<a href="{% url 'userprofile:dashboard' user_profile_detail.slug %}" class="list-group-item {% if request.path == dashboard %}active{% endif %} waves-effect">
<i class="fa fa-pie-chart mr-3"></i>Dashboard
</a>**

<a href="{% url 'userprofile:profile' user_profile_detail.slug %}" class="list-group-item {% if request.path == profile %}active{% endif %} list-group-item-action waves-effect">
<i class="fa fa-user mr-3"></i>Profile</a>

<a href="{% url 'userprofile:watchlist' user_profile_detail.slug %}" class="list-group-item {% if request.path == watchlist %}active{% endif %} list-group-item-action waves-effect">
<i class="fa fa-eye mr-3" aria-hidden="true"></i>WatchList</a>

<a href="{% url 'userprofile:blog' user_profile_detail.slug %}" class="list-group-item {% if request.path == blog %}active{% endif %} list-group-item-action waves-effect">
<i class="fa fa-book mr-3" aria-hidden="true"></i>My Blogs</a>

<a href="{% url 'userprofile:history' user_profile_detail.slug %}" class="list-group-item {% if request.path == history %}active{% endif %} list-group-item-action waves-effect">
<i class="fa fa-history mr-3" aria-hidden="true"></i>My Browsing History</a>
</div>

</div>



MODELS.PY


class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
slug = models.SlugField(blank=True, unique=True)



VIEWS.PY


def dashboard_view(request, slug):
userprofile = Profile.objects.get(slug=slug)
user_viewed_object = userprofile.objectviewed_set.first()
user_blog = userprofile.userblog_set.first()
user_watchlist = userprofile.watchlist_set.first()

context = {
'user_profile_detail': userprofile,
'user_blog': user_blog,
'user_watchlist': user_watchlist,
'user_viewed_object': user_viewed_object,
'page_name': "Dashboard"
}
print(userprofile.slug)
return render(request, 'userprofile/home.html', context)


def profile_view(request, slug):
user_profile_detail = Profile.objects.get(slug=slug)
if request.method == 'POST':
form = ProfileForm(request.POST or None, request.FILES, instance=user_profile_detail)
if form.is_valid():
print(form)
form.save(commit=False)
user_profile_detail = request.user
form.save()
return HttpResponseRedirect('/')

context = {
'profile': user_profile_detail,
'page_name': 'Profile',
'form': form
}
return render(request, 'userprofile/profile.html', context)



URLS.PY


app_name = 'userprofile'

urlpatterns = [
path('<slug:slug>', dashboard_view, name="dashboard"),
path('<slug:slug>/user_profile/', profile_view, name='profile'),
path('<slug:slug>/watchlist/', watchlist_view, name='watchlist'),
path('<slug:slug>/blog/', userblog_view, name="blog"),
path('<slug:slug>/history/', user_history, name="history"),
]



ERROR MESSAGE:


NoReverseMatch at /profile/jacklit/user_profile/
Reverse for 'dashboard' with arguments '('',)' not found. 1 pattern(s) tried: ['profile\/(?P<slug>[-a-zA-Z0-9_]+)$']



profile.html


{% extends 'userprofile/dashboard.html' %}
{% load crispy_forms_tags %}

{% block content %}

<div class="container-fluid mt-5">

{% include 'userprofile/dashboard_head.html' %}

{% include 'userprofile/large_center_modal.html' %}

<div class="card">
<h2 class="my-3 mx-3"><i class="fa fa-user mr-3" aria-hidden="true"></i>My Profile</h2>
<hr >
<div class="row my-5 mx-1">
<div class="col-3 text-center pl-3">
{% if profile.profile_picture %}
<img src="{{ profile.profile_picture.url }}" alt=""
class="img-fluid z-depth-1-half rounded-circle">
{% endif %}
<div style="height: 10px"></div>
<p class="title mb-0">{{ profile.first_name }} {{ profile.last_name }}</p><br>
<p class="title mb-0" style="font-size: 13px">{{ profile.role }}</p><br>
<p class="title mb-0"><b>Joined: </b>{{ profile.joined }}</p><br><br>

{% if request.user == profile.user %}
<a href="#" data-toggle="modal" data-target="#centralModalLGInfoDemo">Edit Profile</a>
{% endif %}

</div>

<div class="col-9">

<h3><u>Biography</u></h3>
<h6>{{ profile.biography }}</h6>
<hr>
<h6><i class="fa fa-envelope mr-3" aria-hidden="true"></i>Email Address: {{ profile.email }}</h6>
<h6><i class="fa fa-phone mr-3" aria-hidden="true"></i>Office Number:{{ profile.office_number }}</h6>
<h6><i class="fa fa-mobile-phone mr-3" aria-hidden="true"></i>Phone Number: {{ profile.phone_number }}</h6>
<br><br>

<h3><u>Contact Information</u></h3>
<h6>Email: {{ profile.email }}</h6>
<h6>Office: {{ profile.office_number }}</h6>
<h6>Phone: {{ profile.phone_number }}</h6><br><br>

<h3><u>Social Medias</u></h3>
<a href="{{ profile.google_link }}"><h6><i class="fa fa-google-plus mr-3"></i>Google Plus:</h6></a>
<a href="{{ profile.facebook_link }}"> <h6><i class="fa fa-facebook mr-3"></i>Facebook: </h6></a>
<a href="{{ profile.twitter_link }}"><h6><i class="fa fa-twitter mr-3"></i>Twitter: </h6></a>
<a href="{{ profile.linkedin_link }}"><h6><i class="fa fa-linkedin mr-3"></i>LinkedIn: </h6></a>

</div>
</div>
</div>
</div>

{% endblock %}



Large_center_Modal.html


<form action="{% url 'userprofile:profile' user_profile_detail.slug %}" enctype="multipart/form-data" method="post">
{% csrf_token %}
<div class="modal fade" id="centralModalLGInfoDemo" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg modal-notify modal-info" role="document">
<!--Content-->
<div class="modal-content">
<!--Header-->
<div class="modal-header">
<p class="heading lead">Update my Profie</p>

<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true" class="white-text">&times;</span>
</button>
</div>

<!--Body-->
<div class="modal-body">
<div class="">
<div class="col-10">{{ form | crispy }}</div>
</div>


</div>

<!--Footer-->
<div class="modal-footer">
<button type="submit" class="btn btn-outline-info waves-effect">Update</button>
</div>
</div>
<!--/.Content-->
</div>
</div>
</form>




1 Answer
1



Your context dict has profile key: 'profile': user_profile_detail,. So in template you should use profile instead of user_profile_detail:


profile


'profile': user_profile_detail,


profile


user_profile_detail


"{% url 'userprofile:dashboard' profile.slug %}"





Sorry my bad i forgot to add that i have another view which renders the html tags which the sidenav uses
– user8795870
Jul 1 at 5:40





@user8795870 OK. Can you add profile.html template. Looks like the error is there.
– neverwalkaloner
Jul 1 at 5:43



profile.html





@user8795870 your profile.html extends and include many other templates. Check if this templates contain something like "{% url 'userprofile:dashboard' profile.slug %}" . You need to fix it per my answer.
– neverwalkaloner
Jul 1 at 5:49



profile.html


"{% url 'userprofile:dashboard' profile.slug %}"





i realised the problem all my templates are sharing the same 'navbar.html' and all the templates uses different name for the context to get the object hence the issue.
– user8795870
Jul 1 at 6:35





Problem has been resolved!! Thank you so much Any tips on i may organised my html templates such that things like that dont happen again? It was very hard to detect the error until u mentioned about the included templates
– user8795870
Jul 1 at 6:44






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

8pWsSH
vM67RhtRw0SGTgxe,B2KZBP0cmt,z3U8,X

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

List of Kim Possible characters