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 7942597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:01:28+00:00 2026-06-04T00:01:28+00:00

I am currently creating my own Tagging app for Django. Here are the models:

  • 0

I am currently creating my own Tagging app for Django.

Here are the models:

class Tag(models.Model):
    name = models.CharField(max_length=100)

class Book(models.Model):
    title = models.CharField(max_length=100)
    tags = models.ManyToManyField(Tag)

I created a form for my model Book and a widget for Tag inputs:

class BookForm(ModelForm):
    class Meta:
        model = Book
        fields = ('title', 'tags')
        widgets = {
            'tags': TagWidget
        }

class TagWidget(Widget):
    """
    Widget for Tag input fields.
    """
    def render(self, name, value, attrs=None):
        final_attrs = self.build_attrs(attrs, name=name)
        final_attrs['class'] = 'tag-input'
        options = []

        # TODO the following tags should be assigned from the model directly
        options.append('tag1');
        options.append('tag2');

        list_element = u'<ul%s>' % flatatt(final_attrs)
        return mark_safe(list_element + '\n<li>' + (u'</li>\n<li>'.join(options)) + '</li>\n</ul>')

Questions

  1. How can i add the currently existing tags to the options array (options.append(...))?
  2. Also, how can i improve the creation of the html code (see last line mark_safe)? I’d like to put all html code somewhere else (preferably in my templates folder).
  • 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-04T00:01:30+00:00Added an answer on June 4, 2026 at 12:01 am
    1. You can pass additional data in widget’s constructor. Something like

      class TagWidget(Widget):
          def __init__(self, initial_tags, *args, **kwargs)
              self.initial_tags = initial_tags
              super(TagWidget, self).__init__(*args, **kwargs)
      
      # ...
      
      widgets = {
          'tags': TagWidget(initial_tags=('one', 'two', 'three'))
      }
      

      Then the data will be accessible in self.initial_tags.

    2. You can use render_to_string function.

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

Sidebar

Related Questions

im currently creating a graph for an app, using coreplot and have a problem
I'm currently creating my own CMS and I have a hard question (I think
I am currently in the process of creating my own blog and I have
The support on this is currently horrible. I'm creating my own theme for a
I am in a compilers class and we are tasked with creating our own
I'm currently working on creating my own blog section.. After brainstorming a little while
Im currently creating my own forum for my website and i have read plenty
I'm currently creating my own iterator for a b-tree, and I'm stuck on how
I am currently new to MFC in terms of creating my own dll and
I'm creating an app where a user can own many venues and each can

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.