abtrini
abtrini

Reputation: 15

Python/Django URLs

Disclaimer.....this is an assignment! Can someone tell me why I am getting

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/%7B%20%25%20url%20'detail'%20user%20%25%20%7D
Using the URLconf defined in bobbdjango.urls, Django tried these URL patterns, in this order:

[name='home']
profile [name='profile']
user_detail/<str:user_id> [name='detail']
The current path, { % url 'detail' user % }, 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.

I think its a problem with my URL but I am not seeing source of error. I would just like to click on the link on the home page and move to the user details page. See code below... My Home page

<body>
    {% block content %}
    <h1>This is home page</h1>
    <div>
        <!-- <a href="{% url 'profile' %}">Your profile</a> -->
        {% for user in users %}
        <a href="{ % url 'detail' user % }">
        <h3>{{user.first_name}}</h3>
        </a> 
        {% endfor %}
       
    </div>
    {% endblock %}

</body>

My main URL

urlpatterns = [
  ## path('admin/', admin.site.urls),
    path('', include('bobbpets.urls') ),
  ##  path('bobbpets/<int:user.id/>', views.userDetails, name='userDetails'),
]

My app URL

urlpatterns = [
  ##  path('admin/', admin.site.urls),
    path('', views.home, name='home'),
   path('profile', views.profile, name='profile'),

    path('user_detail/<str:user_id>', views.userdetail, name='detail'),
]

My Views

def home(request):
    users = User.objects.all()
    return render(request, 'home.html', {'users': users})

def userdetail(request,user_id):
    user = User.objects.get(id=user_id)
    return render(request, 'user_detail.html', {'user': user})

def profile(request):
    return render(request, 'profile.html')

My Model

from django.db import models

class User(models.Model):
    first_name=models.CharField(max_length=30)
    last_name=models.CharField(max_length=30)
    email=models.EmailField()

    def __str__(self):
        return '{} {}'.format(self.first_name, self.last_name)

Upvotes: 0

Views: 249

Answers (2)

JustCarlos
JustCarlos

Reputation: 128

Your template URL should look like this: <a href="{% url 'detail' user %}

Remove the spaces that you have between your percent signs and parenthesis. That will cause an error.

Upvotes: 1

Try this:

<a href="{ % url 'detail' user.id % }">

but I think you should change

'user_detail/<str:user_id>'

as

'user_detail/<int:user_id>'

Upvotes: 1

Related Questions