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

  • SEARCH
  • Home
  • 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 5965283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:35:00+00:00 2026-05-22T19:35:00+00:00

I’m trying to give django a try by developing a simple page where people

  • 0

I’m trying to give django a try by developing a simple page where people can ask something about a product

This is my model, i can create products in the admin area, display the product page, and the form shows up with the fields email, and text.

class Product(models.Model):
    category = models.ForeignKey(Category)
    title = models.CharField(max_length=100)
    text = models.TextField()

class Question(models.Model):
    email = models.CharField(max_length=100)
    product = models.ForeignKey(Product, default=?, editable=False)
    date = models.DateTimeField(auto_now=True, editable=False)
    text = models.TextField()

class QuestionForm(ModelForm):
    class Meta:
        model = Question

But i don’t know how to tell the model which product id the question has to be saved to.

This is my views.py

# other stuff here
def detail(request, product_id):
    p = get_object_or_404(Product, pk=product_id)
    f = QuestionForm()
    return render_to_response('products/detail.html', {'title' : p.title, 'productt': p, 'form' : f},
    context_instance = RequestContext(request))

def question(request, product_id):
    p = get_object_or_404(Product, pk=product_id)
    f = QuestionForm(request.POST)
    new_question = f.save()

    return HttpResponseRedirect(reverse('products.views.detail', args=(p.id,)))

And the URL

urlpatterns = patterns('products.views',
    (r'^products/$', 'index'),
    (r'^products/(?P<product_id>\d+)/$', 'detail'),
    (r'^products/(?P<product_id>\d+)/question/$', 'question')
)

Righ now it works if i put a “1” in the default attribute for the product foreign key in the question model (where the question mark is), it saves the question to the product id 1. But i don’t know what to do to make it save to the current product.

  • 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-05-22T19:35:01+00:00Added an answer on May 22, 2026 at 7:35 pm

    You can either:

    Send product_id as form value

    Make product foreign key in your form a hidden field and set it’s value to primary key value of the product in detail view. This way you don’t need a product_id in your question view URL and arguments since the ID will be passed with POST data. (see link for examples)

    Id would use this option, since you’d have cleaner question URL and you could do more validation in your form on the product.

    or

    Send product_id through URL

    Use reverse in your detail view to build the form action attribute or use url template tag to build the action attribute in form template. This way you need product_id in your question URL and arguments but you don’t need product field in your QuestionForm. Then in question view simply get the product instance and set it as FK value on Question.


    Example:

    Using url template tag in products/detail.html:

    <form action="{% url question product.pk %}" method="post">
     .... 
    </form>
    

    Or use reverse in detail view:

    def detail(request, product_id):
        p = get_object_or_404(Product, pk=product_id)
        f = QuestionForm()
        return render_to_response('products/detail.html', {
            'title' : p.title, 
            'product': p, 
            'action': reverse("question", args=[p.pk,]),
            'form' : f},
        context_instance = RequestContext(request))
    

    your template:

    <form action="{{ action }}" method="post">
     .... 
    </form>
    

    Either way your question view would need a line added which actually sets product instance to Question attribute:

    def question(request, product_id):
        ...
        new_question.product = p
    

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
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
Does anyone know how can I replace this 2 symbol below from the string
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

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.