UpdateView with additional fields in Django - django

I'm building an app where users can submit a ThesisLink, which contains metadata of their MSc or PhD thesis. Before a thesis link is published, a vetting editor must have the possibility to change fields (for example, in the case of a broken link) or outright reject the thesis link. Submitters should be mailed when their thesis link is accepted, accepted with certain changes, or rejected.
I came to the conclusion that I want some sort of UpdateView, where all the fields of the model are already filled out, and ready to be edited by a vetting editor. But I also want fields that are not on the model, like refusal_reason, or editor_comment. And I want to notify users by mail when a change happens.
How to extend the update view to do this? Or should I abandon the UpdateView altogether and build something on top of FormView?
This is what I have so far:
# urls.py
urlpatterns = [
url(r'^vet_thesislink/(?P<pk>[0-9]+)/$', views.VetThesisLink.as_view(), name='vet_thesislink')
]
# views.py
#method_decorator(permission_required(
'scipost.can_vet_thesislink_requests', raise_exception=True), name='dispatch')
class VetThesisLink(UpdateView):
model = ThesisLink
fields = ['type', 'discipline', 'domain', 'subject_area',
'title', 'author', 'supervisor', 'institution',
'defense_date', 'pub_link', 'abstract']
template_name = "theses/vet_thesislink.html"
And in the template:
# templates/theses/vet_thesislink.html
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Update" />
</form>

You will need to create a custom form using ModelForm with additional non-model fields, and use it in UpdateView using the form_class attribute.

Related

Django - Can I use one UpdateView to update fields on separate pages?

Let's say I have a Student model, with name and age fields, and I have a page with a DetailView class showing these fields. Let's say that rather than having one "update" button that will take me to a form to update all fields of my model at once, I want a separate button for each field that takes me to a separate page with a form to update it.
I know how I could do this with a separate HTML file and separate UpdateView class for each field, but it seems like there should be a cleaner way.
In the first HTML file I have two buttons:
Update name
Update age
In the second I have the form:
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit">
</form>
Urls:
urlpatterns = [
path('<int:pk', views.StudentDetailView.as_view(), name="detail"),
path('update_name/<int:pk>', views.StudentUpdateView.as_view(), name="update_name"),
path('update_age/<int:pk>', views.StudentUpdateView.as_view(), name="update_age"),
]
Views:
class StudentUpdateView(UpdateView):
model = models.Student
template_name = 'update_student.html'
I suppose I'm looking for some sort of if statement that I can put in my view, like:
if condition:
fields = ("name",)
elif condition2:
fields = ("age",)
Hopefully this makes sense! Thank you for any help :)
The simplest way to do this is to override the fields in your urls.py file.
urlpatterns = [
path('<int:pk', views.StudentDetailView.as_view(), name="detail"),
path('update_name/<int:pk>', views.StudentUpdateView.as_view(fields=['name']), name="update_name"),
path('update_age/<int:pk>', views.StudentUpdateView.as_view(fields=['age']), name="update_age"),
]
View.as_view accepts keyword arguments, which are used to override the classes attributes for that occurrence.

Forms, Model, and updating with current user

I think this works, but I came across a couple of things before getting it to work that I want to understand better, so the question. It also looks like other people do this a variety of ways looking at other answers on stack overflow. What I am trying to avoid is having the user to have to select his username from the pulldown when creating a new search-profile. The search profile model is:
class Search_Profile(models.Model):
author_of_profile = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,blank=True)
keyword_string = models.CharField(max_length=200)
other_stuff = models.CharField(max_length=200)
The form I ended up with was:
class Search_Profile_Form(ModelForm):
class Meta:
model = Search_Profile
fields = [ 'keyword_string', 'other_stuff']
Where I deliberately left out 'author_of_profile' so that it wouldn't be shown or need to be validated. I tried hiding it, but then the form would not save to the model because a field hadn't been entered. If I was o.k. with a pulldown I guess I could have left it in.
I didn't have any issues with the HTML while testing for completeness:
<form action="" method="POST">
{% csrf_token %}
{{ form.author_of_profile}}
{{ form.keyword_string }}
{{ form.other_stuff }}
<input type="submit" value="Save and Return to Home Page">
</form>
And the View is where I ended up treating the form and the model separated, saving the form first, then updating the model, which is where I think there might be some other way people do it.
def New_Profile(request):
if request.method=='POST':
form = Search_Profile_Form(request.POST)
if form.is_valid():
post=form.save(commit=False)
# here is where I thought I could update the author of profile field somehow with USER
# but If I include the author_of_profile field in the form it seems I can't.
post.save()
#So instead I tried to update the author_of profile directly in the model
current_profile=Search_Profile.objects.last()
current_profile.author_of_profile=request.user
current_profile.save()
return(redirect('home'))
else:
form=Search_Profile_Form()
return render(request, 'mainapp/New_Profile.html', {'form': form})
So a few questions:
For the Foreign Key in author_of_profile field, is it better to use blank=True, or null=True
I ended up using request.user rather than importing from django.contrib.auth.models import User is there any difference?
My real question though, is the above a reasonable way to get form data and update the database with that data and the user? Or am I missing some other way that is more build in?
post=form.save()
current_profile.author_of_profile=request.user
post.save()
return(redirect('home'))
try something like this. save the form to db then change the author again. save(commit=False) will not save the date to db immediately.

Understanding Django and Django FormView

I am trying to create a Django web app that accepts text in a form/textbox, processes it and redirects to a webpage showing the processed text . I have written a half-functioning app and find de-bugging quite challenging because I don't understand most of what I've done. I'm hoping you will help me understand a few concepts, Linking to resources, also appreciated.
Consider this simple model:
class ThanksModel(models.Model):
thanks_text = models.CharField(max_length=200)
Is the only way to set the text of thanks_text through the manage.py shell? This feels like a pain if I just have one piece of text that I want to display. If I want to display a webpage that just says 'hi', do I still need to create a model?
Consider the view and template below:
views.py
class TestView(generic.FormView):
template_name = 'vader/test.html'
form_class = TestForm
success_url = '/thanks/'
test.html
<form action = "{% url 'vader:thanks'%}" method="post">
{% csrf_token %}
{{ form }}
<input type = "submit" value = "Submit">
</form>
I need to create another model, view and html template and update urls.py for '/thanks/' in order for the success_url to redirect correctly? (That's what I've done.) Do I need to use reverse() or reverse_lazy() the success_url in this situation?
Models are used when you are dealing with Objects and Data and DataBases that can contain a lot of information.
For Example A Person would be a model. their attributes would be age, name, nationality etc.
models.py
class Person(models.Model):
Name = models.CharField(max_length=50)
age = models.IntegerField()
nationality = models.CharField(max_length=50)
Thi deals with multiple bits of information for one object. (the object being the person)
A Thank you message would not need this? so scrap the model for the thank you message. just have views where you create the view using a templates and setting the view to a url.
views.py
class TestView(generic.FormView):
template_name = 'vader/test.html' # self explantory
form_class = TestForm # grabs the test form object
success_url = reverse_lazy('vader:thanks') # this makes sure you can use the name of the url instead of the path
def ThanksView(request): # its simple so you don't even need a class base view. a function view will do just fine.
return render(request,"thanks.html")
test.html
<form action = "{% url 'vader:thanks'%}" method="post">
{% csrf_token %}
{{ form }}
<input type = "submit" value = "Submit">
</form>
thanks.html
<h1>Thank you for Submitting</h1>
<h2> Come Again </h2>
url.py
from django.urls import path
from djangoapp5 import views
urlpatterns = [
path('', TestView.as_view(), name='test_form'),
path('thanks/', views.ThanksView, name='vader:thanks'),
]
I haven't tested this but hopefully it helps and guide you in the right direction

How edit data from form django admin

I'm learning Django Framework, and I have a question. To help you understand I will try and explain using the example below:
Suppose that we have some table in db as is:
CREATE TABLE names (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR(100));
And I have the form in Django Admin as is:
<form>
<textarea name="names"></textarea>
<input type="submit" name="sbt" value="Submit">
</form>
User entered something in the input names in the form and submitted it. Then a script catches this data and splits it into an array (str.split("\n")) and in cycle adding to table names!
And I many quetion:
How i can add form to Django Admin?
How i can catch form data and add this data to somethink table in database?
Thanks.
First of all you must create a django model.
Put this code in models.py.
class Names(models.Model):
name = models.CharField(max_length = 100)
Then you must create the admin model.
Put this code in admin.py.
class NamesAdmin(admin.ModelAdmin):
list_display = ['name']
# whatever you want in your admin panel like filter, search and ...
admin.site.register(Names, NamesAdmin)
I think it meet your request. And for split the names you can override save model method and split the names in there. But if you want to have an extra form, you can easily create a django model form.
Put the code somewhere like admin.py, views.py or forms.py
class NamesForm(forms.ModelForm)
class Meta:
model = Names
That's your model and form. So, if your want to add the form to django admin panel you must create a view for it in django admin. For do this create a view as common.
Put the code in your admin.py or views.py.
def spliter(req):
if req.method == 'POST':
form = NamesForm(req.POST)
if form.is_valid():
for name in form.cleaned_data['names'].split(' '):
Names(name = name).save()
return HttpResponseRedirect('') # wherever you want to redirect
return render(req, 'names.html', {'form': form})
return render(req, 'names.html', {'form': NamesForm()})
Be aware you must create the names.html and put the below code in you html page.
{% extends 'admin/base_site.html' %}
{% block content %}
<!-- /admin/names/spliter/ is your url in admin panel (you can change it whatever you want) -->
<form action="/admin/names/spliter/" method="post" >{% csrf_token %}
{{ form }}
<input type="submit" value="'Send'" >
</form>
{% endblock %}
This is your view and your can use it everywhere. But if you want only the admin have permission to see this page you must add this method too your NamesAdmin class.
def get_urls(self):
return patterns(
'',
(r'^spliter/$', self.admin_site.admin_view(spliter)) # spliter is your view
) + super(NamesAdmin, self).get_urls()
That's It. I hope this can help you.

What's the correct way to use django's ModelFormMixin?

I've read through the docs on this and yet I can't seem to get django's ModelFormMixin working properly. This is what I have in urls.py:
...
url(r'^vendors/edit/(?P<pk>\d+)/$', 'vendor_edit', name='vendor_edit'),
...
and in views.py:
class VendorEditView(DetailView, ModelFormMixin):
form_class = VendorForm
model = Vendor
success_url = reverse_lazy('vendor_list')
template_name = 'vendor_edit.html'
and the template:
<form action='.' method='post'>{% csrf_token %}
{{ form }}
<button>Save</button>
</form>
I've tried mixing it into different types of views besides DetailView (View, TemplateView, FormView) with no luck.
What I expect to happen is that when I go to /vendors/edit/1, a form is on the page with the object's details already filled in for editing. What I'm getting is either a blank form, no form, or a 405 HTTP response. Is there something obvious I'm missing here?
Figured it out: Needed to use django's UpdateView with the mixin to get it working.
Edit: Per comments, no need to mix it in to the UpdateView, as it's already included in the inheritance chain.