Let's say I have a CreateArticleView
like the following which extends CreateView
.
class CreateArticleView(CreateView):
model = ArticleModel
fields = ['title', 'text']
ArticleModel
actually has 3 fields, though; it also has category
.
category
is a url argument, as urls follow the following regex: 127.0.0.1/category/(?P<category>[0-z]+)/articles/new/
.
I need some way to get the category
from the URL (this has been answered by SO users) and some way to then take that data and put it into my ArticleModel
instance (this has not).
For instance, let's say a user wants to create an ArticleModel
entitled, "Russia's Military Just Bought Five Bottlenose Dolphins and It Won't Say Why" with the text, "Wow!"
I'd get the title
and text
from the POST (or GET) data and could plug it into the ArticleModel
with article = ArticleModel.objects.create(title=title_from_POST, text=text_from_POST)
. However, I'd also need to provide the category
kwarg for the ArticleModel
. How would I plug that in?
Please note: I am not making a website where people can publish articles. The code above is entirely for example. What I'm looking for is some way to get URL variables to class-based functions.