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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:24:39+00:00 2026-05-25T06:24:39+00:00

I have a form: class AdForm(ModelForm): class Meta: model = Ad widgets = {

  • 0

I have a form:

class AdForm(ModelForm):
    class Meta:
        model = Ad
        widgets = {
            'title': TextInput(attrs={'class': 'small'}),
            'link': TextInput(attrs={'class': 'large'}),
            'code': Textarea(attrs={'class': 'limit', 'cols': 60, 'rows': 20}),
            'clicks': TextInput(attrs={'class': 'tiny', 'value': 0}),
        }

Based off of:

TYPE_CHOICES = (
    ('Image', 'Image'),
    ('Code', 'Code'),
)

SIZE_CHOICES = (
    ('Leaderboard', 'Leaderboard'),
    ('Banner', 'Banner'),
    ('Skyscraper', 'Skyscraper'),
    ('Square', 'Square'),
)

class Ad(models.Model):
    title = models.CharField(max_length=40)
    type = models.CharField(max_length=5, choices=TYPE_CHOICES)
    size = models.CharField(max_length=11, choices=SIZE_CHOICES)
    link = models.URLField(null=True)
    media = models.ImageField(null=True, upload_to='ads')
    code = models.TextField(null=True, max_length=500)
    clicks = models.IntegerField()
    created = models.DateTimeField(auto_now_add=True)
    expires = models.DateTimeField(null=True)

    def __unicode__(self):
        return self.name

and basically if the type is Image I want link and media required, however if it is Code I want code required. Is there any way to do this or force validate those fields based on certain conditions?

My current processing method is below:

@login_required
def create(request):
    if request.POST:
        form = AdForm(request.POST)

        if form.is_valid():
            status = 'Success'
        else:
            status = 'Failure'
    else:
        form = AdForm()
        status = 'None'

    template = loader.get_template('ads/create.html')
    context = RequestContext(request, {
        'form': form,
        'status': status
    })

    return HttpResponse(template.render(context))

I looked in the documentation but it said to force save, is there any other better way? Thank you so much from a Django noob.

  • 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-25T06:24:40+00:00Added an answer on May 25, 2026 at 6:24 am

    Take a look at the documentation for forms around validating fields that depend on other fields: https://docs.djangoproject.com/en/1.11/ref/forms/validation/#cleaning-and-validating-fields-that-depend-on-each-other

    Your model will need to allow the fields to be blank so you don’t get required validation errors from the model when certain combinations are invalid. Instead, ensure the correct combination of field values exists at the form level.

    Hope that helps you out.

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

Sidebar

Related Questions

I have a sample form: class AdminDiscountForm(ModelForm): class Meta: model = Discount exclude =
I have this form: class TourForm(ModelForm): class Meta: model = Tour I wanna design
I Have this Form class MyModelForm(forms.ModelForm): boolfield = forms.TypedChoiceField(coerce=bool, choices=((False, 'No'), (True, 'Yes')), widget=forms.RadioSelect
I have a form class that looks like this: class ApplicationDetailsForm(ModelForm): worked_in_industry = forms.TypedChoiceField(coerce=int,
I have a following form: class PlayForwardPageForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(PlayForwardPageForm, self).__init__(*args, **kwargs)
I have a view which queries a model to populate a form: class AddServerForm(forms.Form):
I have form: class ItemForm(forms.ModelForm): id = forms.ModelChoiceField(queryset=Item.objects.all(), widget=forms.HiddenInput()) temp = forms.BooleanField(widget=forms.HiddenInput(), required=False) date
I have form: class AdmItemForm(forms.ModelForm): id = forms.ModelChoiceField(queryset=Article.objects.all(), widget=forms.HiddenInput()) mainimage = forms.ImageField(widget=AdmImageWidget(), required=False) tags
I have form: class Form(forms.ModelForm): id = forms.ModelChoiceField(queryset=Category.objects.all(), widget=forms.HiddenInput()) def choices(self): items = ...query
Assume I have a form class SampleClass(forms.Form): name = forms.CharField(max_length=30) age = forms.IntegerField() django_hacker

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.