在Django

时间:2015-11-28 09:14:18

标签: python django web

现在我想添加一个表单来更新用户的现有歌词。这是我的代码:

urls.py:

from django.conf.urls import url 
from . import views
from django.conf import settings
urlpatterns = [
url(r'^$', views.lyric_list, name = 'lyric_list'),
url(r'^lyric/(?P<lyric_id>[0-9]+)/$', views.lyric_detail, name ='lyric_detail'),
url(r'^add_lyric/$', views.add_lyric, name = "add_lyric"),
url(r'^delete/(?P<pk>\d+)$', views.delete_lyric, name = 'delete_lyric'), 
url(r'^update/$', views.update_lyric, name = 'update_lyric'),
url(r'^update/(?P<pk>\d+)$', views.update_lyric_page, name = 'update_lyric_page'),]

views.py:

@login_required
def update_lyric(request, pk):
  lyric = get_object_or_404(Lyric, pk = pk)
  form = LyricForm(request.POST, initial = {'title': lyric.title, 'body': lyric.body})
  if lyric:
    username = lyric.user.username 
  if form.is_valid():
    title = form.cleaned_data['title']
    body = form.cleaned_data['body']
    lyric.title = title 
    lyric.body = body
    lyric.save()
    update_topic(request.user)
    return HttpResponseRedirect(reverse('rap_song:lyric_list'))
return render(request, 'rap_song/update_lyric_page.html',{'pk':pk})         

@login_required
def update_lyric_page(request, pk):
  lyric = get_object_or_404(Lyric, pk = pk)
  form = LyricForm()
  return render(request, 'rap_song/update_lyric_page.html', {'pk': pk, 'form':form})

update_lyric_page.html:

<div class = "col-md-6 col-md-offset-3">
<form action="{% url 'rap_song:update_lyric' lyric_id %}" method="post" class="form">
  {% csrf_token %}
  {% bootstrap_form form layout="inline" %}
  {% buttons %}
  <button type="submit" class="btn btn-primary">
  {% bootstrap_icon "star" %} Edit
  </button>
  {% endbuttons %}
</form >

但是我收到了一个错误:/ rap_song / update /

中的TypeError

update_lyric()缺少1个必需的位置参数:&#39; pk&#39;

整个Traceback如下:

Traceback:
File "/Users/yobichi/wi/lib/python3.5/site-packages/django/core/handlers/base.py" in get_response
132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/yobichi/wi/lib/python3.5/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
22.                 return view_func(request, *args, **kwargs)

Exception Type: TypeError at /rap_song/update/
Exception Value: update_lyric() missing 1 required positional argument: 'pk'

任何人都可以帮我解决这个问题吗?我一整天都在努力解决这个问题。提前谢谢!

1 个答案:

答案 0 :(得分:1)

您需要传递需要更新的网址中的id歌词:

url(r'^update/(?P<pk>\d+)/$', views.update_lyric, name='update_lyric')

此外,看起来您可以轻松地将update_lyricsupdate_lyrics_page视图合并到一个视图函数中,因此您将在 urls.py 中保存一个额外的网址:

首先,删除以下网址,因为我们不再需要update_lyrics_page

url(r'^update/(?P<pk>\d+)$', views.update_lyric_page, name='update_lyric_page'),

然后,合并这两个视图如下:

@login_required
def update_lyric(request, pk):
    lyric = get_object_or_404(Lyric, pk=pk)
    username = lyric.user.username 
    if request.POST:
        form = LyricForm(request.POST, 
            initial={'title': lyric.title, 'body': lyric.body})
        if form.is_valid():
            title = form.cleaned_data['title']
            body = form.cleaned_data['body']
            lyric.title = title 
            lyric.body = body
            lyric.save()
            update_topic(request.user)
            return HttpResponseRedirect(reverse('rap_song:lyric_list'))
        else:
            return render(request, 
                'rap_song/update_lyric_page.html',{'pk': pk, 'form': form})
    else:
        form = LyricForm()
        return render(request, 
            'rap_song/update_lyric_page.html',{'pk': pk, 'form': form})

最后,我认为模板中没有名为lyric_id的变量。我怀疑它应该被发送的pk变量替换为

<form action="{% url 'rap_song:update_lyric' pk %}" ...

会解析为这样的网址:

  

/rap_song/update/1234/

其中1234是歌词对象的id。