Django中的hmtl网址按钮找不到我的网址'找不到。尝试了1个模式:['edit /(?P <pk> \\ d +)$ / $']'

时间:2019-05-29 19:04:22

标签: python html django

我正在尝试在django1中建立一个按钮,以将PK发送给我,但是却无法正常工作。 我在显示mi数据的屏幕上,没有按钮,一切运行良好,我的对象数组显示了数据,但是当我添加按钮时,它崩溃了

html

{% if alumnos %}
    <ul>
    {% for alumno in alumnos %}
    <td>{{ alumno.dni }}</td>
    <td>{{ alumno.nombre }}</td>
    <td>{{ alumno.apellido1 }}</td>
    <td>{{ alumno.apellido2 }}</td>
    <td>{{ alumno.email }}</td>
    <td>{{ alumno.repetidor }}</td>
    <td>
        <a class="btn btn-secondary" href="{% url 'edit' pk=alumno.dni %}">Editar</a>
    </td>
{% endfor %}

urls.py

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

view.py

def edit_alumno3(request, pk):
    user = Alumno.objects.get(dni=pk)
    if request.method == 'GET':
        form = AlumnoForm2(instance=user)
    else:
        form = AlumnoForm2(request.POST, instance=user)
        if form.is_valid():
            form.save()
        return redirect('edit.html', {'form': form})

    return render(request, '/edit.html', {'form': form})

model.py

class Alumno(models.Model):
    dni = models.CharField(max_length=9,primary_key=True)
    nombre = models.CharField(max_length=100)
    apellido1 = models.CharField('Primer apellido',max_length=50)
    apellido2 = models.CharField('Segundo apellido',max_length=50)
    email = models.EmailField("Correo electronico",null=True)
    repetidor = models.BooleanField()
    curs = models.ManyToManyField(Curso, blank=True, related_name="Historico_de_cursos")
    Nivel = models.ManyToManyField('Nivel', through = 'Completado',through_fields=('Alumno','Nivel'))
    Practica = models.ManyToManyField('Practica', through = 'Nota',through_fields=('Alumno','Practica'))
    Curso = models.ManyToManyField('Curso',through = 'Curso_alumno',through_fields=('Alumno','Curso'))


    def __str__(self):
        return self.dni

错误:

NoReverseMatch at /mostrar_alumnos2/

Reverse for 'edit' with keyword arguments '{u'pk': ''}' not found. 1 pattern(s) tried: ['edit/(?P<pk>\\d+)$/$']

enter image description here

0 个答案:

没有答案