Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8763805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:52:12+00:00 2026-06-13T15:52:12+00:00

So I’m building a basic Q&A site– Each topic has a series of questions

  • 0

So I’m building a basic Q&A site– Each topic has a series of questions associated with it, and each question has multiple answers associated with it.

I’m creating the user input for questions and they have to associated with a topic. This is the questions model

#models.py
class Question(models.Model):
    movie           = models.ForeignKey(Movie, blank=True, null=True)
    question_text   = models.CharField(max_length = 1000)
    question_detail = models.CharField(max_length = 5000, blank = True, null = True)
    q_pub_date      = models.DateTimeField(auto_now_add = True)
    q_author        = models.ForeignKey(User)
class QuestionForm(ModelForm):
    def save(self, user = None, force_insert = False, force_update = False, commit = True):
        q = super(QuestionForm, self).save(commit = False)
        q.q_author = user
        if commit:
            q.save()
        return q

    class Meta:
        model = Question
        exclude = ('movie', 'q_author', 'q_pub_date')

This is the URL conf

#urls.py
url(r'^(?P<movie_id>\d+)/add_question/$', 'add_question'),

Now here is the view

#views.py
def add_question(request, movie_id):
    if request.method == "POST":
        form = QuestionForm(request.POST, request.FILES)
        #QuestionForm.movie = Movie.objects.get(pk = movie_id)
        if form.is_valid():
            form.save(user = request.user)
            return HttpResponseRedirect("/home/")
    else:
        form = QuestionForm()
    return render_to_response("qanda/add_question.html", {'form': form}, context_instance = RequestContext(request))

This is the HTML code

#add_question.html
<h1> Add Question: {{ user.username }}</h1>
    <form action = "" method = "post">{% csrf_token %}
        {{ form.as_p }}
        <input type = "submit" value = "Ask" />
        <input type = "hidden" name = "next" value = "{{ next|escape }}" />
    </form>

In the view, the commented out line is what I added to the view to try and auto save the model. When adding a question, the URL has the ID of the movie it is associated with, and my thought is to take that ID and then plug it into the ForeignKey to identify which movie is associated with the question. However, when I use my code, it changes all of the Questions’ movie associations to the current movie instead of just changing that specific question’s movie association. Without the code, it doesn’t associate a Movie with the Question at all. How do I fix this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:52:13+00:00Added an answer on June 13, 2026 at 3:52 pm

    Use this:

    #views.py
    def add_question(request, movie_id):
        if request.method == "POST":
            form = QuestionForm(request.POST, request.FILES)
            if form.is_valid():
                question = form.save(user = request.user)
                question.movie = Movie.objects.get(pk = movie_id)
                question.save()
                return HttpResponseRedirect("/home/")
        else:
            form = QuestionForm()
        return render_to_response("qanda/add_question.html", {'form': form}, context_instance = RequestContext(request)
    

    For question asked in comment

    You should avoid using absolute URLs in views or templates. Consider a scenario, where you decide to change home URL from /home/ to /myhome/. You will have to edit it where ever you have used them. It is always better to name the urls (docs):

    # URL Conf
    url(r'^home/$', 'home_view', name="home_url"),
    url(r'^(?P<movie_id>\d+)/add_question/$', 'add_question', name="add_question_url"),
    url(r'^home/(?P<movie_id>\d+)/$', 'movie_view', name="movie_url"),
    

    The name argument act as an unique identifier to your actual URLs

    Now in you views:

    from django.core.urlresolvers import reverse
    
    def some_view(request):
        ...
        return HttpResponseRedirect(reverse('home_url'))
    

    Now what ever change you make to the URL (say /home/ to /myhome/ makes no effect to the view as long as the name argument has the same value in the URL conf.

    If you wish to pass parameters (like movie_id in your case)

    def some_view(request, movie_id):
        ...
        return HttpResponseRedirect(reverse('movie_url', kwargs={'movie_id':movie_id}))
    

    The same concept should be used in templates to avoid hard-coding URLS in templates. Please read this for more details

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.