【发布时间】:2020-10-31 11:26:46
【问题描述】:
我有两页个人资料并添加注释。每当我单击任何用户配置文件上的添加注释时,我都想在添加注释表单中填写患者字段,我需要更改 url 路径吗?如果是,我如何从 url 中获取数据并将其填写到表单中。
urls.py
urlpatterns = [
path('', dashboard, name='dashboard'),
path('profile/', profile, name='profile'),
path('profile/<str:slug>/<int:pk>', profile, name='profilepk'),
path('edit_profile/<int:id>/', edit_profile, name='editprofile'),
path('addnotes/', addnotes, name='addnote'),
]
views.py
def addnotes(request):
profile = Profile.objects.get(user_id=id)
form = PatientNotesForm(request.POST or None)
if form.is_valid():
form.save()
return redirect(f'/dashboard/profile/{user.profile.slug}/{user.pk}')
return render(request, 'core/addnotes.html', {'form': form})
def profile(request, slug, pk):
profil = Profile.objects.get(slug=slug)
profile = Profile.objects.get(pk=pk)
context = {'profile': profile, 'profil': profil}
return render(request, 'dashboard/profile.html', context)
models.py
class Note(models.Model):
illness = models.CharField(max_length=1000, blank=True)
patient = models.ForeignKey(Profile, on_delete=models.CASCADE, related_name='patientnote', null=True)
Doctor = models.CharField(max_length=100, blank=True)
Description = models.CharField(max_length=10000, blank=True)
created = models.DateTimeField(auto_now_add=True)
def __str__(self):
return f"{self.illness}"
forms.py
class PatientNotesForm(ModelForm):
illness = forms.CharField(max_length=100, help_text='First Name')
patient = forms.CharField(max_length=100, help_text='Last Name')
doctor = forms.CharField(max_length=100, help_text='address')
description = forms.CharField(max_length=100,widget=forms.Textarea)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['illness'].widget.attrs.update(
{'placeholder': ('illness')})
self.fields['doctor'].widget.attrs.update(
{'placeholder': ("Doctor's name")})
self.fields['patient'].widget.attrs.update(
{'placeholder': ("Patient's name")})
self.fields['description'].widget.attrs.update(
{'placeholder': ("Description")})
self.fields['illness'].widget.attrs.update({'class': 'log'})
self.fields['doctor'].widget.attrs.update({'class': 'log'})
self.fields['patient'].widget.attrs.update({'class': 'log'})
self.fields['description'].widget.attrs.update({'class': 'textarea'})
class Meta:
model = Note
fields = ('illness', 'patient', 'doctor', 'description')
addnotes.html
<form method="post" class="fum"> {%csrf_token%}{{form.illness}}{{form.patient}}{{form.doctor}}{{form.description}}
<li class="btnn "><button type="submit " class="conf ">Confirm</button></li>
</form>
profile.html
<ul class="main-menu">
<li>
<a href="#"><img class="nav-items" src="{% static 'images/home.svg'%}" alt=""><span>Home</span></a>
</li>
<li>
<a href="#"><img class="nav-items" src="{% static 'images/male.svg'%}" alt=""><span>Patients</span></a>
</li>
<li>
<a href="#"><img class="nav-items" src="{% static 'images/doctor.svg'%}" alt=""><span>Add notes</span> </a>
</li>
<li>
<a href="#"><img class="nav-items" src="{% static 'images/lab.svg'%}" alt=""><span>Edit profile</span>
</a>
</li>
</ul>
</div>
【问题讨论】:
-
user_id=id中的id来自哪里。由于那不是参数,它将使用id内置函数,这将不起作用。 -
@WillemVanOnsem 该行无关紧要。我忘了摆脱它。
-
但我说的不是
editprofile,而是addnotes。