I am trying to save the user in the 100 characters 'microblog' minimum application. My code does not have any power, but does not work. The mistake is in views.py, I can not save the foreign key in the user table.
models.py
looks like this:
class NewManager (models.Manager): def create_post (auto, post, user name) : New = self.model (post = post, created_by = username) new.save () returned new class new (models.model): post = models.CharField (MAX_LENGTH = 120) date = models.DateTimeField (auto_now_add = true) ) Created by = created_by = models.ForeignKey (user, empty = true) objects = NewManager () class NewForm (ModelForm): square meta model = new fields = ['post'] # widget = {'post': Textarea (attrs = {' Column ': 80,' rows': 20}) Def save_new (request): If request.method == 'Post': created_ By = user ('Post', '') new_obj = new (post = post, date = date, new_obj.save) Returns HTTPPRPS page reader ect ('/') and: form = NewForm () Return to render_to_response ('news / new_form. Html ', {' form ': form}, context_instance = RequestContext (request))
I did not mention import here - they are doing right, anyway. When I try to save it says:
local variables 'created_by' assignment
before referencing if I created_py You do not show completely the traceback, which will help you to track exactly where the error is occurring (I doubt it is not the exact code that you are running, or this actual error Message is not a result, because the ridge does not match the error code. Most of the requirement.) However, a couple of indicators before, you are not using a form of saving. If you have done so, you probably will not see this error. Instead of getting the prices manually from the request, you should instantiate the form using the Also, keep in mind that your manager is completely irrelevant - the default manager already defines a parameter to save as a parameter, more parameters are required to save. This is really strange.
request.POST
and then save it. Besides, you are not seeing that the form is actually valid before trying to use its values. Here's what you should do:
if request.method == 'Post': form = NewForm (request.POST) if form.is_valid (): new_obj = form.save ( Committed = False) new_obj.created_by = request.user new_obj.save () Returns HttpResponseRedirect ('/') else: form = NewForm () Returns render_to_response ('news / new_form.html', {'form': form}, context_instance = RequestContext (Request))
form
method , Which does the same to you. / P>
Comments
Post a Comment