Django - 如何在按钮点击时删除单个数据库条目?

时间:2016-07-18 13:05:22

标签: python django django-models django-templates django-views

我正在尝试设置用户个人资料,您可以在其中输入技能。输入技能并将其保存在数据库中已经有效。现在我想让用户有机会通过点击按钮删除每一个。我尝试在URL中点击按钮点击每个技能的ID,然后在我的视图中将其读出来获取项目并将其删除,但这不符合我的想法,我无法找到原因......

我的观点

def profile_settings(request, id=None):
# get logged in user object from session
user_id = request.user.id
# get related userprofile
userprofile = UserProfile.objects.get(pk=user_id)
# get all skills of the userprofile
user_skills = Skill.objects.filter(user=userprofile)

if request.method == 'POST':

    form = SkillForm(request.POST)

    if 'delete-skill' in request.POST:
            if id:
                print(id)
                skill = Skill.objects.get(pk=id).delete()

    elif 'add-skill' in request.POST:
        if form.is_valid():
            # get data from form
            name = form.cleaned_data['name']
            category = form.cleaned_data['category']
            rating = form.cleaned_data['rating']
            # create new skill object for a user
            new_skill = Skill(name=name, category=category, rating=rating, user=userprofile)
            # save it in the database
            new_skill.save()

else:
    form = SkillForm()
return render(request, 'profile-settings.html', {'skillform': form, 'existing_skills': user_skills})

我的网址

urlpatterns = [
    url(r'^landing', views.landing, name='landing'),
    url(r'^neuigkeiten', views.news, name='news'),
    url(r'^profileinstellungen/', views.profile_settings, name='profileinstellungen'),
    url(r'^profileinstellungen/(?P<id>\d+)/$', views.profile_settings, name='profileinstellungen'),
]

我的模板

{% extends 'base.html' %}

{% block content %}
    <form method="post" style="margin-top: 300px">
        {% csrf_token %}
        {{ skillform }}

        <input type="submit" value="Hinzufügen" name="add-skill "/>

    </form>

    <form method="post">
        {% csrf_token %}
        <ul>
            {% for skill in existing_skills %}
                <li>{{ skill.name }}</li>
                <input href="{% url 'profileinstellungen' id=skill.id%}" type="submit" value="Löschen" name="delete-skill"/>
            {% endfor %}
        </ul>
    </form>

{% endblock %}

它不会在URL中发布技能的ID。有替代方法吗?

2 个答案:

答案 0 :(得分:0)

您忘记在网址格式的正则表达式末尾包含美元符号。它应该是:

urlpatterns = [
    url(r'^landing$', views.landing, name='landing'),
    url(r'^neuigkeiten$', views.news, name='news'),
    url(r'^profileinstellungen/$', views.profile_settings, name='profileinstellungen'),
    url(r'^profileinstellungen/(?P<id>\d+)/$', views.profile_settings, name='profileinstellungen'),
]

特别是问题是正则表达式r'^profileinstellungen/。如果没有美元,它会匹配/profileinstellungen/4/之类的网址,因此ID不会传递给视图。

其次,给输入一个href属性没有意义。每个输入都应该是一个单独的表单,您可以将表单操作设置为您希望将请求提交到的URL。

<ul>
  {% for skill in existing_skills %}
    <form method="post" action="{% url 'profileinstellungen' id=skill.id%}">
       {% csrf_token %}
       <li>
         {{ skill.name }}
         <input type="submit" value="Löschen" name="delete-skill"/>
       </li>
    </form>
  {% endfor %}
</ul>

答案 1 :(得分:0)

尝试改变这一点:

<input href="{% url 'profileinstellungen' id=skill.id%}" type="submit" value="Löschen" name="delete-skill"/>

由此:

<input href="{% url 'profileinstellungen' %}{{skill.id}}" type="submit" value="Löschen" name="delete-skill"/>

因为&#34; url&#34;会自动知道&#34; skill.id&#34;与&#34; id&#34;相关