1

the title probably doesn't tell my problem, sorry about it.

I have a models.py like this:

class Baslik(models.Model):
    user = models.ForeignKey(User, null=True, blank=True)
    title = models.CharField(max_length=50)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)
    active = models.BooleanField(default=True)

I have a function to add object to Baslik model. I just enter a title and it works fine. This is my forms.py:

class BaslikForm(ModelForm):
    class Meta:
        model = Baslik
        fields = ('title',)

My urls for every object is like this: url(r'^(?P<title>.*)/$', 'tek', name = "tek_baslik") I want to create a new object when user enter its future link if it isn't already created. For instance when user enters /baslik/stack I want to create a "stack" object immediately and render its page that defined in views.py for every object. How can I do this. Any idea would help. Thanks.

malisit
  • 1,253
  • 2
  • 17
  • 36
  • 2
    And what kind of precautions will you use? if for instance someoone starts flooding you with random strings? – petkostas Aug 13 '14 at 15:58
  • I have another model that named as Entry and connected to baslik. It has baslik as foreign key. I can define a function to remove basliks if it's not have entries in it. Also it should be available for users only and if user floods I can ban the user. – malisit Aug 13 '14 at 16:01

2 Answers2

2

Here's an approach you can take:

1) Define a new url structure:

url(r'^baslik/(?P<title>.*)/$', views.baslik_handle, name = "tek_baslik")

2) You don't need the modelform, you can directly handle it through the views. Use get_or_create in views.baslik_handle. e.g.

def baslik_handle(request, title):
   baslik, created = Baslik.objects.get_or_create(title=title)
Community
  • 1
  • 1
Sudip Kafle
  • 4,286
  • 5
  • 36
  • 49
1

The ModelForm is unnecessary, try this:

view

def create_baslik(request, title):
    context = RequestContext(request)
    context_dict = {}

    baslik, created = Baslik.objects.get_or_create(title=title, user=request.user)
    context_dict['baslik'] = baslik

    return render_to_response('baslik.html', context_dict, context)

template

{% if baslik %}
    {{ baslik.title }}
    ...
{% endif %}
crhodes
  • 1,178
  • 9
  • 20