how to feed success url with pk from saved model?

  • Last Update :
  • Techknowledgy :
def get_success_url(self, ** kwargs):
   # obj = form.instance or self.object
return reverse("profile", kwargs = {
   'pk': self.object.pk
})

Suggestion : 2

You expect a primary key on the request object, which makes no sense. The instance (self.object) is where you should retrieve the primary key from.,Django: How To Auto Fill all the Model form with the data on 'load' button click after accepting primary key as text input from the user,How to use choices in a model from a table previously populated with a fixture,How do you Create Groups and Permissions in Django model from Active Directory Groups with django-python3-ldap?

def get_success_url(self, ** kwargs):
   # obj = form.instance or self.object
return reverse("profile", kwargs = {
   'pk': self.object.pk
})

Suggestion : 3

You could add in class anycodings_django-views CreateWordFromSong instead success_url anycodings_django-views method get_success_url,Everything works when I replace song_pk with anycodings_django-views the actual pk.,How to ensure Keycloak brute force detection doesn't occur during user setup,I overwrite form_valid so that the user can anycodings_django-views be saved with the new object. Perhaps there anycodings_django-views is a way I can alter this so that I can also anycodings_django-views get the song_pk? I've played around with it anycodings_django-views a bit, but without luck.

This is my special CreateView:

class CreateWordFromSong(LoginRequiredMixin, generic.CreateView):
   template_name = 'vocab/add_custom_initial.html'
fields = ("target_word", "source_word", etc.)
model = models.Word
from videos.models
import Song

def form_valid(self, form):
   self.object = form.save(commit = False)
self.object.user = self.request.user
self.object.save()
return super(CreateWordFromSong, self).form_valid(form)

success_url = reverse_lazy('videos:song-vocab', kwargs = {
         'pk': song_pk) #how to get song_pk ?

I get the song_pk from my url:

path('song/<int:song_pk>/', views.CreateWordFromSong.as_view(), name='create-song'),

So I should have access to it. But when I anycodings_django-views try adding it to my view:

class CreateWordFromSong(LoginRequiredMixin, generic.CreateView, song_pk)

You could add in class anycodings_django-views CreateWordFromSong instead success_url anycodings_django-views method get_success_url

    def get_success_url(self):
       return reverse_lazy('videos:song-vocab', kwargs = {
          'pk': self.kwargs.get('song_pk')
       })

It will be work, if action in template anycodings_django-views with form be like this

<form action="{% url 'create-song' song_pk=song.pk %}" method="post">

urls.py

path('flow/<int:pk>/mapping',
   mappingColumnOneFlowListView.as_view(), name='mapping-details'),

in my DeleteView i added :

def get_success_url(self):
   pk = self.kwargs['pk'] # Mapping 's PK
flow_pk = MappingField.objects.filter(
   pk = pk).first().fl_id.pk # fl_id.pk = Flow 's PK (FK)
return reverse('mapping-details', kwargs = {
   'pk': flow_pk
})

Suggestion : 4

Feed classes subclass django.contrib.syndication.views.Feed. They can live anywhere in your codebase.,The Feed class subclasses django.contrib.syndication.views.Feed.,The behavior of get_context_data() mimics that of generic views - you’re supposed to call super() to retrieve context data from parent class, add your data and return the modified dictionary.,For the contents of <title> and <description>, Django tries calling the methods item_title() and item_description() on the Feed class. They are passed a single parameter, item, which is the object itself. These are optional; by default, the string representation of the object is used for both.

from django.contrib.syndication.views
import Feed
from django.urls
import reverse
from policebeat.models
import NewsItem

class LatestEntriesFeed(Feed):
   title = "Police beat site news"
link = "/sitenews/"
description = "Updates on changes and additions to police beat central."

def items(self):
   return NewsItem.objects.order_by('-pub_date')[: 5]

def item_title(self, item):
   return item.title

def item_description(self, item):
   return item.description

# item_link is only needed
if NewsItem has no get_absolute_url method.
def item_link(self, item):
   return reverse('news-item', args = [item.pk])
from django.urls
import path
from myproject.feeds
import LatestEntriesFeed

urlpatterns = [
   #...
   path('latest/feed/', LatestEntriesFeed()),
   #...
]
from mysite.models
import Article
from django.contrib.syndication.views
import Feed

class ArticlesFeed(Feed):
   title = "My articles"
description_template = "feeds/articles.html"

def items(self):
   return Article.objects.order_by('-pub_date')[: 5]

def get_context_data(self, ** kwargs):
   context = super().get_context_data( ** kwargs)
context['foo'] = 'bar'
return context
Something about {
   {
      foo
   }
}: {
   {
      obj.description
   }
}
path('beats/<int:beat_id>/rss/', BeatFeed()),
from django.contrib.syndication.views
import Feed

class BeatFeed(Feed):
   description_template = 'feeds/beat_description.html'

def get_object(self, request, beat_id):
   return Beat.objects.get(pk = beat_id)

def title(self, obj):
   return "Police beat central: Crimes for beat %s" % obj.beat

def link(self, obj):
   return obj.get_absolute_url()

def description(self, obj):
   return "Crimes recently reported in police beat %s" % obj.beat

def items(self, obj):
   return Crime.objects.filter(beat = obj).order_by('-crime_date')[: 30]

Suggestion : 5

So far I have come up with the approach below. Here, I am trying to pass a parameter holding the URL of the previous page, from the template for the previous page to urls.py and then onto the view. However, I don't think I am using the correct syntax in urls.py as the value of the previous_url parameter in the view is simply "previous_url".,There are other posts on similar situations but none seem to show how to implement this using a class-based view, so any advice here would be most appreciated.

Link in the template for the table page

<a href="{% url 'entry_detail' item.pk %}?previous_url={{ request.get_full_path|urlencode }}">Edit</a>

URL

path('entry/<int:pk>/edit/', EntryUpdateView.as_view(previous_url="previous_url"), name='entry_update'),

View

class EntryUpdateView(LoginRequiredMixin, UpdateView):
   model = Entry
template_name = 'entry_update.html'
fields = ('source', 'target', 'glossary', 'notes')
previous_url = ""
def form_valid(self, form):
   obj = form.save(commit = False)
obj.updated_by = self.request.user
obj.save()
return HttpResponseRedirect(self.previous_url)

Error

Page not found(404)
Request Method: GET
Request URL: http: //localhost:8000/entry/5131/edit/previous_url