Django网址。单击按钮并重定向到同一页面

时间:2017-06-16 08:25:43

标签: django date url redirect controller

HeJ小鼠, 我的网址有问题。通过按钮我传递给url相应的id,在控制器中我获取此ID并将其分配到当前日期以将其保存在数据库中(表格与用户然后管理员点击"参加& #34;并将lastAttendance保存在数据库中)。我点击了网址,因为点击后我没有将我重定向到同一页面。任何建议都赞赏!

MODEL:

class Donor(models.Model):
firstName = models.CharField(max_length=50)
lastName = models.CharField(max_length=50)
bloodType = models.CharField(max_length=10)
createdDate = models.DateTimeField(auto_now_add=True)
lastAttendance = models.DateTimeField(auto_now_add=True)

URLS:

url(r'^donors/get?attend=(\d+)/$', views.donors,  name='donors'),

查看:

@login_required(login_url="login/")
def donors(request):
    global message
    if request.method == 'POST':
        form = PostForm(request.POST) # A form bound to the POST data
        if form.is_valid():
            form.save() 

# context = RequestContext(request)
donor_id = None
if request.method == "GET":
    donor_id = request.GET.get('id')

if donor_id:
    donor = Donor.objects.get(id=int(donor_id))
    if donor:
        donor.lastAttendance = datetime.datetime.now()
        donor.save()

#display all the lates added donors
query_results = Donor.objects.order_by('-createdDate')
#dislay total ammount of rows
donors_count = Donor.objects.count()

return render(request, 'donors.html', {
    'form': PostForm(), 
    'donors_count': donors_count,
    'query_results': query_results,       
    })

HTML:

  <button 
type="submit" 
class="btn attendBtn" 
value="{{ item.id }}" 
name="attend">attend

OUTPUT output in the browser

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

您不应在网址中使用问号,因为它被视为网址的参数。试试这个:

url(r'^donors/get/attend/(\d+)/$', views.donors,  name='donors'),

这将导致诸如

之类的URL
https://example.com/donors/get/attend/12456