Django Class Based View for both Create and Update

I ran into a situation where I wanted something like this. Here’s what I came up with (do note that if you’re trying to use it as an update view and it can’t find the requested object, it’ll behave as a create view rather than throwing a 404): from django.views.generic.detail import SingleObjectTemplateResponseMixin from django.views.generic.edit import … Read more

django createview how to get the object that is created

maybe you could use get_success_url() method (see reference) In this case, it’d be something like: def get_success_url(self): return reverse(‘offerta_create’,args=(self.object.id,)) Please see the accepted answer on Why doesn’t self.object in a CreateView have an id after saving to the database? – once I removed the “id” fields from my models, and let Django default to its … Read more

How to set ForeignKey in CreateView?

I solved this by overriding form_valid method. Here is verbose style to clarify things: class CreateArticle(CreateView): model = Article def form_valid(self, form): article = form.save(commit=False) article.author = self.request.user #article.save() # This is redundant, see comments. return super(CreateArticle, self).form_valid(form) Yet we can make it short (thanks dowjones123), this case is mentioned in docs.: class CreateArticle(CreateView): model … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)