Beispiel #1
0
def insert_blog(request):
    if request.method == 'POST':

        blog_form = BlogForm(request.POST)
        if blog_form.is_valid():

            blog_name_from_user = blog_form.cleaned_data['blog_name']
            blog_tagline_from_user = blog_form.cleaned_data['blog_tag_line']

            blog_object = Blog(name=blog_name_from_user,
                               tagline=blog_tagline_from_user)
            blog_object.save()  # will save the data from the form to database

            blog_object = Blog()
            blog_object.name = "Blog"
            blog_object.tagline = "Lorem ipsum dolor sit amet"
            blog_object.save(
            )  # will save the data from the form to database table blog

            entry_object = Entry()
            entry_object.headline = "Lorum Ipsum"
            entry_object.body_text = """Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."""
            entry_object.pub_date = "2018-07-04"
            entry_object.mod_date = "2018-07-04"
            entry_object.n_comments = 0
            entry_object.n_pingbacks = 0
            entry_object.rating = 5
            entry_object.blog = blog_object  # will insert the blog id into blog_entry
            entry_object.save()

            john = Author.objects.create(
                name="John")  # will create new author in blog_authers table
            paul = Author.objects.create(
                name="Paul")  # will create new author in blog_authers table
            george = Author.objects.create(
                name="George")  # will create new author in blog_authers table

            entry_object.authors.add(
                john, paul, george)  # will add data to blog_entry_authors
            return HttpResponse('Data Inserted successfully')

    else:
        blog_form = BlogForm(request.POST)
        return render(request, 'blog-insert-form.html', {'form': blog_form})