gpt4 book ai didi

python - 如何在 Django/Django Rest Framework 中使用已选择的主键 (pk) 继续新 View 的 URL

转载 作者:太空宇宙 更新时间:2023-11-03 21:23:42 26 4
gpt4 key购买 nike

从我的索引 View 中,我列出了数据库中可用的“俱乐部”,当用户选择一个俱乐部时,它会将他们重定向到俱乐部主页。我已经包含了一个球员注册页面,但我不知道如何根据用户之前在索引中选择的俱乐部来呈现此页面。所以我想做的是输出像这样的url:http://127.0.0.1:8000/account/club_home/3/player_registration/ “3”是所选俱乐部的主键。

url.py:

url(r'^club_home/$', views.club_home, name='club_home'),
url(r'^club_home/(?P<pk>\d+)/$', views.club_home, name='club_home_with_pk'),
url(r'^club_home/player_registration/$', views.RegisterPlayer.as_view(), name='player_register'),

views.py:

class Index(TemplateView):
template_name = 'index.html'

def get(self, request):
users = User.objects.all()
clubs = ClubInfo.objects.all()

args = {'users': users,
'clubs': clubs}
return render(request, self.template_name, args)


def club_home(request, pk=None):
if pk:
club = ClubInfo.objects.filter(pk=pk)
user = request.user
else:
club = ClubInfo.objects.filter(user=request.user)
user = request.user
args = {'club': club,
'user': user
}
return render(request, 'club_home_page.html', args)

class RegisterPlayer(APIView):
renderer_classes = [TemplateHTMLRenderer]
template_name = 'player_registration.html'

def get(self, request):
serializer = PlayerRegistrationSerializer()
return Response({'serializer': serializer})

def post(self, request):
serializer = PlayerRegistrationSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response({'serializer': serializer})

型号:

from django.db import models
from django.contrib.auth.models import User

class ClubInfo(models.Model):

user = models.OneToOneField(User, on_delete=models.CASCADE)
club_name = models.CharField(max_length=50, default='')
club_logo = models.ImageField(upload_to='profile_pics', blank=True)
club_address1 = models.CharField(max_length=30)
club_address2 = models.CharField(max_length=30, default='')
club_address3 = models.CharField(max_length=30, default='')
club_town = models.CharField(max_length=30)
club_county = models.CharField(max_length=30)
club_country = models.CharField(max_length=30)
# slug = models.SlugField(max_length=150, unique=True)

def __str__(self):
return self.club_name


class PlayerRegistration(models.Model):

club_name = models.ForeignKey(ClubInfo, on_delete=models.CASCADE)
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
dob = models.DateField(max_length=8)
email = models.EmailField(max_length=50)
phone = models.CharField(max_length=12)
mobile = models.CharField(max_length=15)
emergency_contact_name = models.CharField(max_length=40)
emergency_contact_mobile = models.CharField(max_length=15)
address1 = models.CharField(max_length=30)
address2 = models.CharField(max_length=30, default='')
address3 = models.CharField(max_length=30, default='')
town = models.CharField(max_length=30)
county = models.CharField(max_length=30)
country = models.CharField(max_length=30)

def __str__(self):
return "%s %s" % (self.first_name, self.last_name)

序列化器:

 class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('url', 'username', 'email', 'groups')


class PlayerRegistrationSerializer(serializers.ModelSerializer):

class Meta:
model = PlayerRegistration
fields = '__all__'
labels = {
'dob': ('D.O.B'),
}
widgets = {
'dob': DateInput(attrs={'type': 'date'})
}

最佳答案

您应该保留 club_home 端点 URL 模式并添加 player_registration,这样您调用的 View (views.RegisterPlayer) 就会收到club_home 主键作为参数:

url(r'^club_home/(?P<pk>\d+)/player_registration/$', views.RegisterPlayer.as_view(), name='player_register')

关于python - 如何在 Django/Django Rest Framework 中使用已选择的主键 (pk) 继续新 View 的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54011595/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com