我想通过创建一个基于用户模型的搜索表单来扩展我的django博客应用,目前任何配置文件页面都只能由当前登录的用户访问,简单的基本网址对应以下urlpattern。
urlpatterns = [
path('profile/', views_register.profile, name='profile'),
]
我正在从下面的html模板中获取用户搜索查询。
<form method="GET" action="" id="searchform">
<input class="searchfield" id="searchbox" name="q" type="text" value="{{ request.GET.q }}" placeholder="Search..."/>
</form>
在我的views.py中,我有一个函数来接收这个查询,但我不知道如何将搜索用户重定向到相应的用户配置文件,因为每个用户的配置文件页面没有任何特定的基于用户名的自定义URL。
class ProfileSearch(LoginRequiredMixin):
template_name = 'home.html'
def get_queryset(self, request):
query = request.GET.get('q')
if query:
return User.objects.filter(username__icontains=query)
else:
return User.objects.all()
我如何将查询发起者重定向到这个新的个人资料页面?我知道这个功能没有任何作用,因为它不做任何动态重定向,但我毫无头绪。
以下是配置文件模型,当有新用户注册时,通过信号自动创建。
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
image = models.ImageField(default= 'default.jpg', upload_to='profile_pics')
def __str__(self):
return f'{self.user.username} Profile'
def save(self, *args, **kwargs):
super().save(*args, **kwargs)
img = Image.open(self.image.path)
if img.height > 300 or img.width > 300:
output_size = (300, 300)
img.thumbnail(output_size)
img.save(self.image.path)