1👍
It may be because of forgetting a simple space, of course, I encountered this error just today and two hours later I realized that my problem is a simple space… space between equal sign and variable….
{% if room.host.username == request.user.username %}
<div>
<a href="{% url 'update-room' room.id %}">Edit</a>
<a href="{% url 'delete-room' room.id %}">Delete</a>
</div>
{% endif %}
0👍
It is because pk
is of int
type not type.str
Simply change your all routes to <int:pk>/
.
Try below code:
Urls.py
urlpatterns = [
path('login/', views.loginPage, name="login"),
path('logout/', views.logoutUser, name="logout"),
path('register/', views.registerPage, name="register"),
path('', views.home, name="home"),
path('room/<int:pk>/', views.room, name="room"),
path('profile/<int:pk>/', views.userProfile, name='user-profile'),
path('create-room/', views.createRoom, name="create-room"),
path('update-room/<int:pk>/', views.updateRoom, name="update-room"),
path('delete-room/<int:pk>/', views.deleteRoom, name="delete-room"),
path('delete-message/<int:pk>/', views.deleteMessage, name="delete-message"),
Note:
Always give/
at the end of every route in path function.
Note:
Function based views are generally written insnake_case
notcamelCase
, so it will better if you change your view name touser_profile
instead of.userProfile
- [Answered ]-Sorting sizes with two or more numbers in Django
- [Answered ]-Backbone.js for in-browser page layout editor?
0👍
I think the error is happening on this line in feed_component.html:
<a href="{% url 'user-profile' room.host.id %}">@{{room.host.username}}</a>
room.host.id
is resolving to a blank string, which is caused by a room object with a null host field.
The Room
model definition does allow for a null host:
class Room(models.Model):
host = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
- [Answered ]-Why can't django set attributes outside the database?
- [Answered ]-Django south: problems with path to django
0👍
This problem might have occurred because you deleted a user who was having rooms. since you deleted the user’s room which sets the room’s host to null
"host = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)"
I have just resolved this problem; it seems to be a problem due to your rooms not having any host. that’s why it is not able to find the user profile for a particular room. just go to your Django admin and check all your room’s hosts, if it is null then set any user to its host problem will be solved.
- [Answered ]-Django Dynamically Add Forms Based On Key Value Pairs List
- [Answered ]-Send selection from bootstrap dropdown button with Django
- [Answered ]-Django: unable to login with new users
- [Answered ]-How can I upload two images in a form to 2 different directories in django?