class CityCreate(CreateView):
model = Cities
fields = ['city_name', 'tags']
template_name = 'countries/new_city.html'
def get_success_url(self):
return reverse('countries:cities', args=[self.country.id])
def get_context_data(self, **kwargs):
self.country = get_object_or_404(Countries, id=self.kwargs['country_id'])
kwargs['country'] = self.country
return super().get_context_data(**kwargs)
def form_valid(self, form):
self.country = get_object_or_404(Countries, id=self.kwargs['country_id'])
form.instance.country = self.country
messages.success(self.request, 'The city has been added to the list of visited places, thank you')
return super().form_valid(form)