First, I will search up the user, and then it takes me to a page of a list of users to click onto. After that, when I choose a user I want to click on to check out their profile, it should take me to users page I want to see, but that is where I have a problem. The URL pattern should be, for example /user/MatthewRond/, but instead, I get /user/MatthewRond Profile/, which prevents me from seeing Matthew's page because Profile was never intended to be in the URL. If I can figure out how to get rid of the Profile, my problem should be solved.
I read up on what was and what I got from it: it has to do with using and dealing with alphanumeric characters. Regardless none of it helped me figure out how to remove it. As for the code part of it, I am grabbing the default username from the database then grabbing the custom profile I made from that default user. After that, I set the username to the custom profile model I made.
views.py
def profile_view(request, username, *args, **kwargs,):
context = {}
try:
user = User.objects.get(username=username)
profile = user.profile
img = profile.uploads_set.all().order_by("-id")
context['username'] = user.username
except:
return HttpResponse("Something went wrong.")
if profile and img:
context = {"profile": profile, "img": img}
return render(request, "main/profile_visit.html", context)
search_results.py
<a href="{% url 'profile_view' username=profile.0 %}">
urls.py
path("user/<str:username>/", views.profile_view, name = "profile_view"),
models.py
class Profile(models.Model):
user = models.OneToOneField(User, on_delete = models.CASCADE, null = False, blank = True)
first_name = models.CharField(max_length = 50, null = True, blank = True)
last_name = models.CharField(max_length = 50, null = True, blank = True)
phone = models.CharField(max_length = 50, null = True, blank = True)
email = models.EmailField(max_length = 50, null = True, blank = True)
bio = models.TextField(max_length = 300, null = True, blank = True)
profile_picture = models.ImageField(default = 'default.png', upload_to = "img/%y", null = True, blank = True)
banner_picture = models.ImageField(default = 'bg_image.png', upload_to = "img/%y", null = True, blank = True)
def __str__(self):
return f'{self.user.username} Profile'
CodePudding user response:
With the help of Rapheal, the error would be in the Profile class. Change this
def __str__(self):
return f'{self.user.username} Profile'
to this.
def __str__(self):
return f'{self.user.username}'
Remove the space and the Profile